Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

variables problem

0 views
Skip to first unread message

mike

unread,
Dec 6, 2005, 1:48:11 PM12/6/05
to
I need to have some global variables and am having some trouble getting
them defined correctly.

key parts of my pgm looks like:

use strict:

my %files = ();

&do_something

#display the contents of the hash
foreach my $key ( sort keys %files )
{
print "$files{$key}{uid} $file_cand{$key}{fileid}\n";
}

sub do_something
{
$files{'frank'} = {uid=>'frank',fileid=>'12345'};
$files{'mark'} = {uid=>'mark',fileid=>'23456'};
}

In the sub do_something it is telling me I have to explicitly define
at:
$files{'frank'} = {uid=>'frank',fileid=>'12345'};

Isn't that defined when I defined the hash?

Mike

A. Sinan Unur

unread,
Dec 6, 2005, 1:55:23 PM12/6/05
to
"mike" <hil...@charter.net> wrote in news:1133894891.262669.157220
@o13g2000cwo.googlegroups.com:

> I need to have some global variables and am having some trouble
> getting them defined correctly.
>
> key parts of my pgm looks like:

What is the point of writing 'pgm' rather than 'program'?

I very much doubt key parts of your program look like, say,

> use strict:

this.

Also,

use warnings;

missing.

> my %files = ();
>
> &do_something

Why are you using an ampersand here?

Where is the semi-colon?



> #display the contents of the hash
> foreach my $key ( sort keys %files )

Do you really need to sort the keys?

> {
> print "$files{$key}{uid} $file_cand{$key}{fileid}\n";
> }

%file_cand is not defined.

> sub do_something
> {
> $files{'frank'} = {uid=>'frank',fileid=>'12345'};
> $files{'mark'} = {uid=>'mark',fileid=>'23456'};
> }
>
> In the sub do_something it is telling me I have to explicitly define
> at:

What is telling you? Did perl give you an error or warning message?

> $files{'frank'} = {uid=>'frank',fileid=>'12345'};
>
> Isn't that defined when I defined the hash?

Huh?

Please post a short but *complete* script that still exhibits the
problem you are encountering. Instructions for how to put together an
inteligent post can be found in the posting guidelines for this group.

Sinan

--
A. Sinan Unur <1u...@llenroc.ude.invalid>
(reverse each component and remove .invalid for email address)

comp.lang.perl.misc guidelines on the WWW:
http://mail.augustmail.com/~tadmc/clpmisc/clpmisc_guidelines.html

Paul Lalli

unread,
Dec 6, 2005, 2:05:15 PM12/6/05
to
mike wrote:
> I need to have some global variables

Why? What makes you think you need globals? And furthermore, if your
question is about global variables, why are there no global variables
anywhere in your example script?

> and am having some trouble getting them defined correctly.
>
> key parts of my pgm looks like:

Please do not use lazy spelling. Not everyone here is a native English
speaker. I promise you won't get carpal-tunnel from typing the extra
r, o, r, and a.

> use strict:

That's a syntax error. Please do not re-type your code. Copy and
paste it.

You are forgetting 'use warnings;'

> my %files = ();
>
> &do_something

That's a syntax error. Please do not re-type your code. Copy and
paste it.

Why are you using an ampersand there? Do you know what that does? If
not, remove it, and use
do_something();
instead.

> #display the contents of the hash
> foreach my $key ( sort keys %files )
> {
> print "$files{$key}{uid} $file_cand{$key}{fileid}\n";

That's a syntax error. Please do not re-type your code. Copy and
paste it.

> }
>
> sub do_something
> {
> $files{'frank'} = {uid=>'frank',fileid=>'12345'};
> $files{'mark'} = {uid=>'mark',fileid=>'23456'};
> }
>
> In the sub do_something it is telling me I have to explicitly define
> at:

Don't paraphrase warning and error messages. They are important. Copy
and paste the exact text of the error message.

> $files{'frank'} = {uid=>'frank',fileid=>'12345'};
>
> Isn't that defined when I defined the hash?

Perhaps one or more of the syntax errors noted above is the root cause
of your program. Without seeing the exact text of the message you
claim to have received, we cannot know for sure.

Correct all three errors, re-run your program, and copy & paste the
code AND error message if you still want assistance.

... on second thought, before you do any of that, read the Posting
Guidelines for this group. They're posted here twice a week. Just
search for "Posting Guidelines".

Paul Lalli

Purl Gurl

unread,
Dec 6, 2005, 2:06:19 PM12/6/05
to
mike wrote:

(snipped)

> I need to have some global variables and am having some trouble getting
> them defined correctly.

Your code contains fatal syntax errors. Do not post fatally flawed code.

> use strict:

There is no need to use strict for such a simple script.

> my %files = ();

our ($key, %file_cand, %files);

Use of my declarations for globals serves no purpose.
Use of our declarations for globals serves no purpose.

Strict is a broken module. Unless your skills are such you truly
need the "help" strict affords, do not use strict.

Use of strict benefits many who need extra help coding. However,
if you use strict, research and read about strict to learn in what
ways that module is broken, and to learn about problems created
by using strict.

Purl Gurl

Purl Gurl

unread,
Dec 6, 2005, 2:24:13 PM12/6/05
to
Purl Gurl wrote:

> mike wrote:

(snipped)

> > I need to have some global variables and am having some trouble getting
> > them defined correctly.

If it is you would rather use lexical "private" variables,
here is a rewrite of your code which does just such.

Use of global variables is perfectly fine, and every script
does have global variables, visible or not. However, a good
practice is to use as few global variables as is reasonable
so perl core can reuse memory blocks. Note that is "reuse"
memory not "save" memory as is often claimed.

A decision each code writer "should" make is use of globals
more reasonable than use of lexical private variables. Often
this decision is made based simply on how many times
you need to use a variable, in various parts of a script.

In short, minimize use of global variables as is reasonable.

You should note use of lexical private variable for this
example script, serves no viable purpose. Perl may
reuse some memory, but reuse is so little as to
not be measurable.

For a short script like this, there is no need to use
lexical variables. Contrasting this, if this example
is part of a much larger program, then use of lexical
variables "might" be viable.

Purl Gurl

#!perl

use strict;

&do_something;

sub do_something
{
my ($key, %files, %file_cand);


$files{'frank'} = {uid=>'frank',fileid=>'12345'};
$files{'mark'} = {uid=>'mark',fileid=>'23456'};

foreach $key ( sort keys %files )

mike

unread,
Dec 6, 2005, 2:45:31 PM12/6/05
to
Perl Gurl ... Thank you very much.

In the past many of your forum experts ( so called ) always told me to
"use strict". If I didn't they cried.

It's probably those same experts that always "complain" about how a
post is made, or how stupid the poster must be, or they didn't dot the
i or cross the t.

Biggest bunch of babies I've ever seen.

What I was really trying to do is:
1) using strict define a global hash variable,
2) then update that hash in a subroutine,
3) print the contents of that hash when I return from the subroutine,
4) then calling another different subroutine and iterate over the hash
and do something else.

Thanks again. It's been awhile since I've been to this forum and now I
can remember why.

Mike

Purl Gurl

unread,
Dec 6, 2005, 3:21:45 PM12/6/05
to
mike wrote:

> Biggest bunch of babies I've ever seen.

Cry babies looking for any excuse to cry.


> What I was really trying to do is:

> 1) using strict define a global hash variable,

Use our for that. Use of our serves no purpose
but will prevent strict from crying.

> 2) then update that hash in a subroutine,

In a subroutine is where you most often use lexical
private variables. Pay attention to newly created
variables, those are often not noticed! That is what
bit you on your butt, previously.

> 3) print the contents of that hash when I return from the subroutine,

I tend to keep a print out in the open, using global variables. Printing
is often the final goal of a script. Keeping your prints highly visible
helps a lot when you review your code.

> 4) then calling another different subroutine and iterate over the hash
> and do something else.

Yes, those are circumstances where you will want to consider use
of lexical private variables.

Some, though, practice overkill with lexical variables. This is very
true when you call a series of subroutines and need to pass data
to all, or between subroutines. Lexical variables make that a chore,
and often are less efficient. This is when global variables become
quite useful.

Your use of &subroutine is perfectly acceptable, contrary to
claims. Just be careful about prototype considerations, this
is, your global @_ array might be affected.

One of the greatest challenges in programming is making
decisions about which methods should be used, or not used.
When you arrive at a point of skill when you question if a
method is best or not, then you are becoming truly skilled.

Purl Gurl

Brad Baxter

unread,
Dec 6, 2005, 4:40:28 PM12/6/05
to
mike wrote:
> Perl Gurl ... Thank you very much.

In my not so humble opinion, are you sure that
you are thanking the right person? Would you
rather have someone be nice and give you bad
advice, or be short (from your perspective) and
give you good advice?

> In the past many of your forum experts ( so called ) always told me to
> "use strict". If I didn't they cried.

I don't think you'll find anyone calling himself
a forum expert here. Except perhaps Ms. Gurl.

> It's probably those same experts that always "complain" about how a
> post is made, or how stupid the poster must be, or they didn't dot the
> i or cross the t.
>
> Biggest bunch of babies I've ever seen.

Not worth responding to. Even though I did. :-)

> What I was really trying to do is:
> 1) using strict define a global hash variable,
> 2) then update that hash in a subroutine,
> 3) print the contents of that hash when I return from the subroutine,
> 4) then calling another different subroutine and iterate over the hash
> and do something else.

And did you not get a response?

> Thanks again. It's been awhile since I've been to this forum and now I
> can remember why.

Buck up. You can take it.

Regards,

Brad

A. Sinan Unur

unread,
Dec 6, 2005, 4:59:07 PM12/6/05
to
"mike" <hil...@charter.net> wrote in news:1133898331.070981.285170
@f14g2000cwb.googlegroups.com:

> Perl Gurl ... Thank you very much.
>
> In the past many of your forum experts ( so called ) always told me to
> "use strict". If I didn't they cried.
>
> It's probably those same experts that always "complain" about how a
> post is made, or how stupid the poster must be, or they didn't dot the
> i or cross the t.
>
> Biggest bunch of babies I've ever seen.

Enjoy your private dialog with PG.

*PLONK*

Sherm Pendley

unread,
Dec 6, 2005, 5:25:35 PM12/6/05
to
"mike" <hil...@charter.net> writes:

> Perl Gurl ... Thank you very much.
>
> In the past many of your forum experts ( so called ) always told me to
> "use strict". If I didn't they cried.

Oh, so you'd rather have warm fuzzies than accurate guidance?

OK, then, I guess I won't be helping you.

*plonk*

sherm--

--
Cocoa programming in Perl: http://camelbones.sourceforge.net
Hire me! My resume: http://www.dot-app.org

Eric J. Roode

unread,
Dec 6, 2005, 10:41:58 PM12/6/05
to
"Purl Gurl" <purl...@purlgurl.net> wrote in
news:feidnSXRoYzVfAje...@giganews.com:

> mike wrote:
>
> (snipped)
>
>> I need to have some global variables and am having some trouble getting
>> them defined correctly.
>
> Your code contains fatal syntax errors. Do not post fatally flawed code.
>
>> use strict:
>
> There is no need to use strict for such a simple script.

That may be true, but I'd wager that the OP needs all the help he can get
:-)

--
Eric
`$=`;$_=\%!;($_)=/(.)/;$==++$|;($.,$/,$,,$\,$",$;,$^,$#,$~,$*,$:,@%)=(
$!=~/(.)(.).(.)(.)(.)(.)..(.)(.)(.)..(.)......(.)/,$"),$=++;$.++;$.++;
$_++;$_++;($_,$\,$,)=($~.$"."$;$/$%[$?]$_$\$,$:$%[$?]",$"&$~,$#,);$,++
;$,++;$^|=$";`$_$\$,$/$:$;$~$*$%[$?]$.$~$*${#}$%[$?]$;$\$"$^$~$*.>&$=`

0 new messages