×
INTELLIGENT WORK FORUMS
FOR COMPUTER PROFESSIONALS

Log In

Come Join Us!

Are you a
Computer / IT professional?
Join Tek-Tips Forums!
  • Talk With Other Members
  • Be Notified Of Responses
    To Your Posts
  • Keyword Search
  • One-Click Access To Your
    Favorite Forums
  • Automated Signatures
    On Your Posts
  • Best Of All, It's Free!
  • Students Click Here

*Tek-Tips's functionality depends on members receiving e-mail. By joining you are opting in to receive e-mail.

Posting Guidelines

Promoting, selling, recruiting, coursework and thesis posting is forbidden.

Students Click Here

Jobs

IO::Handle - how to create a new filehandle

IO::Handle - how to create a new filehandle

IO::Handle - how to create a new filehandle

(OP)
Hi,

I seem unable to open a new filehandle object so I can print some data to it?

I have...

CODE

# open io handle          
        my $io_handle = IO::Handle->new();
        
        if ($io_handle->fdopen(fileno("XLS"),"w")) 
        {                            
                
            # output column headings to io_handle object
            $io_handle->print($xls);

      }
      else {die "failed to open filehandle";} 

It fails unable to open the file handle.

What am I doing wrong?

Thanks,
1DMF

"In complete darkness we are all the same, it is only our knowledge and wisdom that separates us, don't let your eyes deceive you."

"If a shortcut was meant to be easy, it wouldn't be a shortcut, it would be the way!"

Free Dance Music Downloads

RE: IO::Handle - how to create a new filehandle

Hi

fileno's parameter should be a file handle. What is your intention with that "XML" string there ?

Feherke.
feherke.github.io

RE: IO::Handle - how to create a new filehandle

(OP)
I'm trying to create an IO::Handle object I can spit my XLS data to because Catalyst can't handle scalar data in this context.

It will accept an IO::Handle object.

So I want to put my XLS data into an IO::Handle object via my model, so I can pass it to the Catalyst controller to output the view.

So how to I create a filehandle for the IO::Handle module?



"In complete darkness we are all the same, it is only our knowledge and wisdom that separates us, don't let your eyes deceive you."

"If a shortcut was meant to be easy, it wouldn't be a shortcut, it would be the way!"

Free Dance Music Downloads

RE: IO::Handle - how to create a new filehandle

(OP)
Well I even tried passing in a properfile handle...

CODE

------------------------------------------------------------------
      my $xls = "col1,col2,col3\n";	
 
        # open io handle          
        $io_handle = IO::Handle->new();
        open my ($str_fh), '>', \$xls;
        
        if ($io_handle->fdopen($str_fh,"w")) 
        {   
	$io_handle->print('"row1","row2","row3"' . "\n");
       }

   return $io_handle; 

but all I get is a blank XLS returned from Catalyst?

All I want to do is print some CSV data to the browser, this is doing my head in!

"In complete darkness we are all the same, it is only our knowledge and wisdom that separates us, don't let your eyes deceive you."

"If a shortcut was meant to be easy, it wouldn't be a shortcut, it would be the way!"

Free Dance Music Downloads

RE: IO::Handle - how to create a new filehandle

Hi

Not sure, but according to IO::Handle's description this may be the wrong way :

Quote (IO::Handle)

IO::Handle is the base class for all other IO handle classes. It is not intended that objects of IO::Handle would be created directly, but instead IO::Handle is inherited from by several other classes in the IO hierarchy.
So I would try IO::File instead. Seems to be descendant of IO::Handle, so that Catalyst should be happy with it.

Feherke.
feherke.github.io

RE: IO::Handle - how to create a new filehandle

(OP)
Hi feherke,

It would appear so, when I was told to use an IO::Handle object, it sort of sent me of in the wrong direction.

What I need to use is IO::File!

At least I was right, when I couldn't work out how to create the file handle for the IO::Handle object smile

Thanks for the input.

Regards,
1DMF

"In complete darkness we are all the same, it is only our knowledge and wisdom that separates us, don't let your eyes deceive you."

"If a shortcut was meant to be easy, it wouldn't be a shortcut, it would be the way!"

Free Dance Music Downloads

RE: IO::Handle - how to create a new filehandle

(OP)
Well I've had to give up, I cannot get IO::File to work?

CODE

use IO::File;
my $xls = "col1,col2,col3\n";
my $io = new IO::File \$xls, "w";
print $io '"row1col1","row1col2","row1col3"' . "\n";
$io->close; 

I pass $io back to Catalyst and all I get is an empty spreadsheet.

I'm stumped!

"In complete darkness we are all the same, it is only our knowledge and wisdom that separates us, don't let your eyes deceive you."

"If a shortcut was meant to be easy, it wouldn't be a shortcut, it would be the way!"

Free Dance Music Downloads

RE: IO::Handle - how to create a new filehandle

Hi

Why using $xls as reference ? It seems to be a scalar :

CODE --> ( fragment )

my $io = new IO::File $xls, "w"; 
This way your above code will work, however not sure what is the final goal.

What is that Catalyst anyway ? Has any documentation on-line ?

Feherke.
feherke.github.io

RE: IO::Handle - how to create a new filehandle

(OP)
Hi feherke,

Well all the info I can find on using a string scalar as a file shows it should be passed as a reference?

I switched my model back to returning the XLS as a scalar and then open it as readonly as an IO::File object in the Catalyst controller and it is now working...

CODE

my ($result,$xls) = $c->model('NBCSXLSExport')->csv_export;    
        
        # check for ok status
        if($result->ok)
        {
            
            # output header
            $c->response->content_type('application/vnd.ms-excel');
            $c->response->content_length(length($xls));            
            $c->response->header(Content_Disposition => 'attachment;filename=NBCS_Export.csv');
                                  
            # create an IO::File for Catalyst
            use IO::File;
            my $iof = IO::File->new;
            $iof->open(\$xls, "r");
                                              
            # output XLS data                              
            $c->response->body($iof);
            
            # close file
            undef $iof;
        }
        else
        {
            $c->response->body( $result->message );              
        }    
        
        $c->response->status(200); 

N.B. Catalyst is an MVC framework for Perl : http://www.catalystframework.org/

You'll notice , I am still passing the scalar as a reference to the IO::File open method and it is working.

Perhaps to open for writing it needs to be the scalar and to open for reading it needs to be a reference?

I'm not sure if it would be better to have my model return the IO::File or leave it as it is with the controller wrapping it up in the IO:File object?

Any thoughts on this regarding the MVC paradigm?

"In complete darkness we are all the same, it is only our knowledge and wisdom that separates us, don't let your eyes deceive you."

"If a shortcut was meant to be easy, it wouldn't be a shortcut, it would be the way!"

Free Dance Music Downloads

RE: IO::Handle - how to create a new filehandle

Hi

Quote (1DMF)

You'll notice , I am still passing the scalar as a reference to the IO::File open method and it is working.
Wondering what exactly is csv_export returning. Strange, anyway.

FYI : Do not wait for my answer on the MVP questions. I am not MVC fan. Hopefully someone versed will jump in.

Feherke.
feherke.github.io

RE: IO::Handle - how to create a new filehandle

(OP)

Quote:

Wondering what exactly is csv_export returning. Strange, anyway.

It is returning a CSV string for EXCEL (XLS).

The model receives post data, generates a recordset based on the user's post data, processes the recordset into a CSV string and returns it for output as an XLS file attachment.

What so strange?

Quote:

FYI : Do not wait for my answer on the MVP questions. I am not MVC fan. Hopefully someone versed will jump in.

Any reason why you don't like MVC?

I thought it was considered the standard paradigm for object orientated programming?



"In complete darkness we are all the same, it is only our knowledge and wisdom that separates us, don't let your eyes deceive you."

"If a shortcut was meant to be easy, it wouldn't be a shortcut, it would be the way!"

Free Dance Music Downloads

Red Flag This Post

Please let us know here why this post is inappropriate. Reasons such as off-topic, duplicates, flames, illegal, vulgar, or students posting their homework.

Red Flag Submitted

Thank you for helping keep Tek-Tips Forums free from inappropriate posts.
The Tek-Tips staff will check this out and take appropriate action.

Reply To This Thread

Posting in the Tek-Tips forums is a member-only feature.

Click Here to join Tek-Tips and talk with other members!

Close Box

Join Tek-Tips® Today!

Join your peers on the Internet's largest technical computer professional community.
It's easy to join and it's free.

Here's Why Members Love Tek-Tips Forums:

Register now while it's still free!

Already a member? Close this window and log in.

Join Us             Close