Why declare "my $something" on top if it's going to get a lexical scope in a subroutine?

G

GreenLeaf

(e-mail address removed) wrote:

sub _transformToHex {
my @data = @_
But why do you have to do that, since we declare "my @data" inside a sub
routine block?
I don't understand this...

Error is due to the missing semicolon after @_. Perl clearly says there
is a syntax error there. I, as a newbie, have seen that it helps to
correct errors in the order they appear, especially when it comes to
syntax errors. :)

Hope this helps,
sat
 
S

strangeuser

Hi --

I've read some explanations in the Camel book, but this error is puzzling me:

------------------------------
code
-----------------------------

#!/usr/bin/perl

use warnings;
use strict;

system("clear");

our $FileName;
our @File;
our @Card;


_enterCard();

my $hex = _transformToHex(@Card);
print "$hex\n";

sub _enterCard
{ #write the biblographic card
print "AUTHOR(S)= "; my $authors = <>;
print "TITLE= "; my $title = <>;
print "KEYWORDS= "; my $keywords = <>;
print "SOURCE/JOURNAL= "; my $source = <>;
print "VOL= "; my $vol = <>;
print "YEAR= "; my $year = <>;
print "PAGES= "; my $pages = <>;
print "EDITOR= "; my $editor = <>;
print "ETC= "; my $etc = <>;
print "X-REF= "; my $xref = <>;

$authors = "AUTHOR(S)= $authors";
$title = "TITLE= $title";
$keywords = "KEYWORDS= $keywords";
$source = "SOURCE/JOURNAL= $source";
$vol = "VOL= $vol";
$year = "YEAR= $year";
$pages = "PAGES= $pages";
$editor = "EDITOR= $editor";
$etc = "ETC= $etc";
$xref = "X-REF= $xref";


@Card=($authors, $title, $keywords, $source, $vol, $year, $pages, $editor, $etc, $xref);
print "\n\n@Card\n";

}


sub _transformToHex {
my @data = @_
my $hex = unpack("H*", join('_', @data));
#print "$hex\n";
return $hex;
}

----------------------
end code
----------------------

----------------------
error
----------------------

$ perl writeFile.pl
syntax error at writeFile.pl line 51, near "@_
my "
Global symbol "@data" requires explicit package name at writeFile.pl line 51.
Execution of writeFile.pl aborted due to compilation errors.
----------------------
end error
---------------------

The data (@Card) was passed to the argument stack (@_) via a subroutine and
it was bound locally (that is, in the block lexical scope) to @data.
Of course, simply appending "my @data;" at the beginning of the script
(next to "our (etc..)") would resolve the error.
But why do you have to do that, since we declare "my @data" inside a sub
routine block?
I don't understand this...


Any comment on what the issue is is appreciated.
TIA.

Henry Lenzi
 
P

Peter Wyzl

Hi --

I've read some explanations in the Camel book, but this error is puzzling me:

sub _transformToHex {
my @data = @_

missing colon after @_ means this line does not parse. (hence @data is not
declared, which causes the second error) Insert the ; and the second error
will disappear too...
my $hex = unpack("H*", join('_', @data));
#print "$hex\n";
return $hex;
}

$ perl writeFile.pl
syntax error at writeFile.pl line 51, near "@_
my "
Global symbol "@data" requires explicit package name at writeFile.pl line 51.
Execution of writeFile.pl aborted due to compilation errors.

fix the errors in the order they appear (generally). Often subsequent
errors are caused by previous ones, especially missing ;. Once those are
fixed, the rest often are fixed too..

P
--
 
T

Tad McClellan

I've read some explanations in the Camel book, but this error is puzzling me:


You are missing a semicolon.

our $FileName;
our @File;
our @Card;


Why are you using package variables instead of lexical variables?

sub _enterCard
{ #write the biblographic card
print "AUTHOR(S)= "; my $authors = <>;
print "TITLE= "; my $title = <>;
print "KEYWORDS= "; my $keywords = <>;
print "SOURCE/JOURNAL= "; my $source = <>;
print "VOL= "; my $vol = <>;
print "YEAR= "; my $year = <>;
print "PAGES= "; my $pages = <>;
print "EDITOR= "; my $editor = <>;
print "ETC= "; my $etc = <>;
print "X-REF= "; my $xref = <>;

$authors = "AUTHOR(S)= $authors";
$title = "TITLE= $title";
$keywords = "KEYWORDS= $keywords";
$source = "SOURCE/JOURNAL= $source";
$vol = "VOL= $vol";
$year = "YEAR= $year";
$pages = "PAGES= $pages";
$editor = "EDITOR= $editor";
$etc = "ETC= $etc";
$xref = "X-REF= $xref";


@Card=($authors, $title, $keywords, $source, $vol, $year, $pages, $editor, $etc, $xref);
print "\n\n@Card\n";

}


You can rewrite that to elimintate a lot of the repetitive code
using a "hash slice":

sub _enterCard { # untested!
my %items;
my @fields = qw/ AUTHOR(S) TITLE KEYWORDS
SOURCE/JOURNAL VOL YEAR
PAGES EDITOR ETC X-REF
/;

foreach my $prompt ( @fields ) {
print "$prompt= ";
$item{$prompt} = $prompt . <STDIN>;
}

@Card = @items{ @fields };
}

sub _transformToHex {
my @data = @_


Where's the semicolon?
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,578
Members
45,052
Latest member
LucyCarper

Latest Threads

Top