CGI/Perl Guide | Learning Center | Forums | Advertise | Login
Site Search: in

  Main Index MAIN
INDEX
Search Posts SEARCH
POSTS
Who's Online WHO'S
ONLINE
Log in LOG
IN

Home: Perl Programming Help: Beginner:
can't declare a simple list from a book example...

 



Wildcard
Novice

Jun 10, 2017, 8:39 AM

Post #1 of 9 (2349 views)
can't declare a simple list from a book example... Can't Post

Hello there! While I told myself it would be nice to actually learn "by the book", I got myself introduction to Perl by the O'reily series. Well there's the problem. While most expalmes just worked dandy, I wrote this code:


Code
($fred, $barney, $dino) = ("Flintstone", "Geroellheimer", undef);

just compiling the code does nothing, when using warnings it says "requires explicit package " and such.. I tried to execute this with the print command but with no avail, so what gives?


BillKSmith
Veteran

Jun 10, 2017, 9:17 AM

Post #2 of 9 (2347 views)
Re: [Wildcard] can't declare a simple list from a book example... [In reply to] Can't Post

The code you show works fine. Your problem must be somewhere else.

Code
C:\Users\Bill\forums\guru>type wildcard.pl 
use warnings;
($fred, $barney, $dino) = ("Flintstone", "Geroellheimer", undef);
$, = "\n";
print( $fred, $barney, $dino );

C:\Users\Bill\forums\guru>wildcard.pl
Flintstone
Geroellheimer
Use of uninitialized value $dino in print at C:\Users\Bill\forums\guru\wildcard.
pl line 4.


Note: I have shown a complete program and a single run (Including all output) of it. The message about "uninitialized value" is correct. This is what your book intended to demonstrate. My use of $, is only to separate the print fields. Try removing it.
Good Luck,
Bill


Wildcard
Novice

Jun 10, 2017, 9:23 AM

Post #3 of 9 (2345 views)
Re: [BillKSmith] can't declare a simple list from a book example... [In reply to] Can't Post

$, = "\n";
that's what was missing, also in the book. Although I must admit I never seen the "$," .. what does it do?


Laurent_R
Veteran / Moderator

Jun 10, 2017, 10:17 AM

Post #4 of 9 (2343 views)
Re: [Wildcard] can't declare a simple list from a book example... [In reply to] Can't Post

"$," is the list output separator; its value will be inserted between each item of a printed list. Setting it to "\n" makes it possible to print each item of the list on its own new line. If you set it to a single space " ", the items will be printed on the same line, but separated by a space.


Wildcard
Novice

Jun 10, 2017, 10:22 AM

Post #5 of 9 (2342 views)
Re: [Laurent_R] can't declare a simple list from a book example... [In reply to] Can't Post

much obliged :)


BillKSmith
Veteran

Jun 10, 2017, 3:36 PM

Post #6 of 9 (2332 views)
Re: [Wildcard] can't declare a simple list from a book example... [In reply to] Can't Post

I do not know what your original problem was. I certainly did not have anything to do with "$,". As I suggested before, try removing that statement. Everything will still work, but not look as good.

You can find the documentation for $, and all the rest of perl's "special variables" by typing at your command line:

Code
perldoc perlvar

or by linking to perlvar
Good Luck,
Bill


Laurent_R
Veteran / Moderator

Jun 11, 2017, 1:12 AM

Post #7 of 9 (2325 views)
Re: [Wildcard] can't declare a simple list from a book example... [In reply to] Can't Post

Back to your original post, yes, indeed:

Code
($fred, $barney, $dino) = ("Flintstone", "Geroellheimer", undef);

does nothing visible by itself. It only assigns values to the $fred and $baney variables, but you would need to do something with these variables so see an effect. For example, you could print them to the screen (see below).

And, if you use warnings (which you should always do), then you need to change the above code line to something like this:


Code
my ($fred, $barney, $dino) = ("Flintstone", "Geroellheimer", undef);

If you do that, you'll no longer have the "required explicit package..." warnings, since "my" declares these variables.

So the overall fix for your problem might be something like this:


Code
use warnings; 
my ($fred, $barney, $dino) = ("Flintstone", "Geroellheimer", undef);
print "Fred: $fred \n";
print "Barney: $barney \n";

The above code should work without any problem. But you'll get a warning again if you try to print out $dino, because it is now declared, but still undefined.

Update: it would help if you said which book exactly (title + edition) you're using.


(This post was edited by Laurent_R on Jun 11, 2017, 1:13 AM)


Wildcard
Novice

Jun 11, 2017, 7:01 AM

Post #8 of 9 (2314 views)
Re: [BillKSmith] can't declare a simple list from a book example... [In reply to] Can't Post


In Reply To
I do not know what your original problem was. I certainly did not have anything to do with "$,". As I suggested before, try removing that statement. Everything will still work, but not look as good.


yeah, I realized now.. it had to do with the <use strict> statement, somehow it prevented me to write this code:


Code
($foo, $fubar, $foobar) = (<AndSoOn>);  
$, = "\n";
print ($foo, $foobar, $fubar);

If I remove strict, then it works, pretty funny Laugh
I guess this is the one who demands the <my> statement.

I tried the same thing with some pop&push operators and the same, it works when I:
1) use strict, then declare variables with my
2) don't use strict and no my-statement necessary.

So here is the question. The book mentions that I "shall use always warnings and strict and for safety measures also implement the #!/usr/bin/perl (I am from a bash scripting background, so it makes sense to me). But isn't <use strict> completely optional and unnecessary feat?


(This post was edited by Wildcard on Jun 11, 2017, 7:02 AM)


BillKSmith
Veteran

Jun 11, 2017, 12:16 PM

Post #9 of 9 (2304 views)
Re: [Wildcard] can't declare a simple list from a book example... [In reply to] Can't Post

You are right that 'strict' is optional. Like 'warnings' its purpose is to help catch common errors, but it does it in a different way. It demands that we declare all variables. (There are a few things that it will not allow, but do not worry about that yet.)

Without strict, all variables that are not declared default to be package (global) variables. With strict, we must declare them, so we usually declare them (with 'my') to be lexical variables. In very simple programs, you probably will not notice any difference. (In the rare cases that you need a package variable, declare it with 'our' instead of 'my'.) If we make a mistake in spelling or capitalization of a variable name, under strict we get an error message referring directly to the error. Without strict perl silently creates a new global variable.

To understand the different types of variables, you must understand the concept of 'scope'. This can be difficult. I am sure that your text book explains it better than I can. You may be tempted to ignore this section. DO NOT!

When lexical variables go out of scope, they cease to exist. If we accidentally try to use one, we receive a message that the variable is not decaled. It is legal to declare all your variables at the start of your program. They would never go out of scope so we would lose most of the benefit of lexical variables.
Good Luck,
Bill

 
 


Search for (options) Powered by Gossamer Forum v.1.2.0

Web Applications & Managed Hosting Powered by Gossamer Threads
Visit our Mailing List Archives