Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

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

Status
Not open for further replies.

1DMF

Programmer
Jan 18, 2005
8,795
GB
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
 
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
 
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
 
Hi

Not sure, but according to [tt]IO::Handle[/tt]'s description this may be the wrong way :
IO::Handle said:
[tt]IO::Handle[/tt] is the base class for all other IO handle classes. It is not intended that objects of [tt]IO::Handle[/tt] would be created directly, but instead [tt]IO::Handle[/tt] is inherited from by several other classes in the IO hierarchy.
So I would try [tt]IO::File[/tt] instead. Seems to be descendant of [tt]IO::Handle[/tt], so that Catalyst should be happy with it.

Feherke.
feherke.github.io
 
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 :)

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
 
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
 
Hi

Why using $xls as reference ? It seems to be a scalar :
Code:
[b]my[/b] [navy]$io[/navy] [teal]=[/teal] new IO[teal]::[/teal]File [highlight][navy]$xls[/navy][/highlight][teal],[/teal] [green][i]"w"[/i][/green][teal];[/teal]
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
 
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 :
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
 
Hi

1DMF said:
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 [tt]csv_export[/tt] returning. Strange, anyway.

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

Feherke.
feherke.github.io
 
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?

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
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top