Beefy Boxes and Bandwidth Generously Provided by pair Networks
Just another Perl shrine
 
PerlMonks  

Sub can't get $_ value?

by DaWolf (Curate)
on Jan 11, 2004 at 23:00 UTC ( [id://320548]=perlquestion: print w/replies, xml ) Need Help??

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

I'm developing an app that manages Excel files so each user can open a different, serialized file.

It has to add zeros to a number at some point (so the filename becomes nicely formatted), so I've made this into a sub. This sub is called from another sub that starts the process of creating and opening the Excel documents.

So this is the sub who does the job:
sub geraNova { my $ctrl = './control.dat'; open(CTR, "+>>$ctrl"); flock(CTR, 2); my $id = <CTR>; flock(CTR, 8); close(CTR); $id = $id + 1; unlink $ctrl; open(CTR, "+>>$ctrl"); flock(CTR, 2); print CTR $id; flock(CTR, 8); close(CTR); $id = addZeros($id); #The above calls the sub passing the id to format my $file = 'C:\Lobo\Sites\test'.$id.'.xls'; my $Excel = Win32::OLE->GetActiveObject('Excel.Application') || Wi +n32::OLE->new('Excel.Application'); $Excel->{DisplayAlerts} = 0; $Excel->{Visible} = 1; my $Book = $Excel->Workbooks->Add(); $Book->SaveAs($file); my $ActBook = $Excel->Workbooks->Open("$file") || die "Não foi pos +sível abrir a planilha. Erro: $!"; undef $Excel; undef $Book; undef $ActBook; }
And this is the one who should add zeros to the control number wich was extracted from a text file:
sub addZeros { my $num = $_; # Below is the line that I'm having problems. #Perl tells me: Use of uninitialized value in length while(length($num) < 6) { $num = "0".$num; } return $num; }
I know I must be missing something very basic, but I still don't get it... Help, please.

my ($author_nickname, $author_email) = ("DaWolf","erabbott\@terra.com.br") if ($author_name eq "Er Galvão Abbott");

Replies are listed 'Best First'.
•Re: Sub can't get $_ value?
by merlyn (Sage) on Jan 11, 2004 at 23:05 UTC
      Thanks, I was very dumb...

      Best regards,

      my ($author_nickname, $author_email) = ("DaWolf","erabbott\@terra.com.br") if ($author_name eq "Er Galvão Abbott");
Re: Sub can't get $_ value?
by kutsu (Priest) on Jan 11, 2004 at 23:05 UTC

    why don't you just get rid of length and use this

    $num = sprintf("%06d", $num); # if only numbers $num = sprintf("%06s", $num); # if num and letters

    "Cogito cogito ergo cogito sum - I think that I think, therefore I think that I am." Ambrose Bierce

      Great tip.

      Thanks!

      my ($author_nickname, $author_email) = ("DaWolf","erabbott\@terra.com.br") if ($author_name eq "Er Galvão Abbott");
Re: Sub can't get $_ value?
by Roger (Parson) on Jan 11, 2004 at 23:26 UTC
    Or you do the following instead (as I often do in my sub's).
    my $num = shift;

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others avoiding work at the Monastery: (4)
As of 2024-04-24 21:15 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found