Beefy Boxes and Bandwidth Generously Provided by pair Networks
There's more than one way to do things

Further On Down the Line

by socrtwo (Sexton)
on Apr 11, 2009 at 22:50 UTC ( #757062=perlquestion: print w/replies, xml ) Need Help??

socrtwo has asked for the wisdom of the Perl Monks concerning the following question:

Hi thanks to Perls of Wisdom and a little help from Dmitry Ovsyanko, I'm farther down the line with my new simple script for converting xlsx2csv. Now I have a new error:

"error: No filename given at line 33"

Here's the code again:

#!/usr/local/bin/perl use Spreadsheet::XLSX; use Spreadsheet::XLSX::Utility2007; use Tk; my $mw = new MainWindow; my $mbar = $mw -> Menu(); $mw -> configure(-menu => $mbar); my $file = $mbar -> cascade(-label=>"File", -underline=>0, -tearoff => + 0); $file -> checkbutton(-label =>"Open", -underline => 0, -command => [\&menuopenClicked, "Open"]); $file -> command(-label =>"Save", -underline => 0, -command => [\&menusavedClicked, "Save"]); $file -> separator(); $file -> command(-label =>"Exit", -underline => 1, -command => sub { exit } ); MainLoop; sub menuopenClicked { my $typesopen = [ ['Excel 2007 files', '.xls'], ['All files', '*'],]; my $mainfilepath = $mw->getOpenFile(-filetypes => $typesop +en, -defaultextension => '.xls'); my $regions = '1-A1:G:110' ; my $rotate = 1; open($mainfilepath, "> $mainfilepath"); my $filename = $mainfilepath; close $mainfilepath; my $csvtxt = Spreadsheet::XLSX::Utility2007::xls2csv($filename, $regio +ns, $rotate); sub menusavedClicked { my $typesaved = [ ['Excel 2007 files', '.xlsx'], ['All files', '*'],]; my $saved = $mw->getSaveFile(-filetypes => $typessaved, -defaultextension => '.csv'); open($saved, "> $saved") || die "Can't create <$saved> for output!\n"; print ($saved $csvtxt) if $saved; close $saved; }}
I have tried various methods to get rid of this error, like putting single or double quotes around $filename or removing the statement:
open($mainfilepath, "> $mainfilepath");
I also tried loading an xls file instead of an xlsx file, after all the function is called xls2csv for some reason. That's the way the script is now, but it doesn't make a bit of difference. Additionally I copied both xls and xlsx files to the root, C:\ in my Windows installation thinking it might be a path issue. Neither file worked and neither file has a space in the name. It seems I am defining the filename with $filename, what's wrong?

Replies are listed 'Best First'.
Re: Further On Down the Line
by BerntB (Deacon) on Apr 11, 2009 at 23:17 UTC

    Your problems are with basic open/read/write/close to files. The first parameter to open should not be the file name... it should be a file handle.

    It is better to split the second parameter in two. See this simple example:

    # Write to text file: open(my $fh, ">", $filename) or die "Failed open for write! $!"; print $fh "Foo\n"; # Prints to file close $fh; # Close file # Read from text file: open($fh, "<", $file_to_read) or die "Couldn't open! $!"; my $line = <$fh>; # Reads a line. chomp $line; # Removes eol chars (CR,LF) close $fh;

    It is much better to write small test programs to check things like this out. I personally use oneliners all the time, like this:

    perl -e 'open($fh, ">", "tst") or die "$!"; print $fh "XX\n"; close $f +h;'

    What you really need is a good first book. There is a review link at the top of this page, it is better to go there, but you won't go wrong with "Perl Cookbook". Otherwise, for a quick look, try the shell cmds:

    perldoc -f open perldoc perldoc perldoc perlrun perldoc Tk # (Added as Edit.)
    (There is also a nice website,

    Edit: I'm thinking of trying out Devel::REPL instead of one-liners. It seems neat.

Re: Further On Down the Line
by ikegami (Pope) on Apr 11, 2009 at 23:09 UTC

    You're using the same var name for the file handlename and the file handle.

    open($mainfilepath, "> $mainfilepath");

    I don't know if that's your problem, but it definitely needs fixing. It needs error checking too, if only or die $!

    Update: I guess I made a similar mistake! Fixed.

Re: Further On Down the Line
by NetWallah (Canon) on Apr 11, 2009 at 23:09 UTC
    Shouldn't that be:
    my $mainfilepath = $mw->getOpenFile(-filetypes => $typesopen, -defaultextension => '.xlsx'); ## xls => + xls_x_

    Also some error-checking after getting the file name, as well as 'open' would be nice.
 maintain is to slowly feel your soul, sanity and sentience ebb away as you become one with the Evil.

Log In?

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://757062]
Approved by Bloodnok
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others scrutinizing the Monastery: (3)
As of 2021-12-07 07:36 GMT
Find Nodes?
    Voting Booth?
    R or B?

    Results (33 votes). Check out past polls.