in reply to Re: Re: Re: Many strings make one variable?
in thread Many strings make one variable?
No, you cant call it that. Or at least if you can then you can also call it "brown pudding". :-)
Dynamic variables exist in a package. So what you mean is that they are members of the package "main".
so there's %ENV, %GET, %POST, %IP, %DEFAULTS, and %DATABASE. This is safer anyway. measure twice, declare once. I'm still not saying i'm right, but anti-soft reference arguments don't float. Maybe it's the hash.
Ok, first off, %ENV is a special variable. Unless fully qualified to be in a seperate package, such as %Foo::ENV it always refers to the %main::ENV aka %::ENV aka %ENV. Next, there is _absolutely_ no reason to set these variables up as dynamic variables if you are creating a single script solution. In fact good practice is that even in the later case you still leave them as lexicals, and provide subroutine accessors for them.
I wonder if a reminder of the beauty of 'local' would also help the case here.
No it wouldn't. Most of the readers here, (no offense) understand local and its uses and misuses better than you do. Try getting the basics down a little better before you play with fire.
After reading dominus's arguments against soft references i still remain. One could spend hours telling you how dangerous using perl is when you use the shebang line.
You completely missed the point of Dominus's writing didnt you? Doing something dangerous when you don't have a choice is ok. Doing something dangerous when you have safer (and more powerful) alternatives is foolish.
Then, i thought of 'local' and did a test... Error 500 in the face.
I perused perldiag and did not find "Error 500" listed. A little further searching revealed that you most likely are reffering to a HTTP error. Ok, you are a beginner CGI programmer, even possibly a script kiddie. Thats ok, in Perlmonks we welcome you, but dont expect us to let bad practice be advised without shooting you down. Stick around, a few months hanging out and youll be a better programmer for it.
This doesn't work.
Well tell it to take its lazy ass off the couch and get a job. :-) Ok humour aside, "doesnt work" is about one of the fastest ways to get people to yell at you. I assume you mean it doesnt do what you want it to do. Which I can only infer from the code...
Ok, well why doesnt it do what you want it to do? Well first turn on strict and warnings. Then rewrite it so that it compiles under them. Then apply some rudimentary programming skills to resolve it.#!/usr/bin/perl &tryLocal("name=james&color=red&age=12"); exit; #################### sub tryLocal{ local $in = $_[0]; @in = split(/\&/,$in); foreach $li(@in){ ($lname,$lvalue)=split(/\=/,$li); local $$lname = $lvalue; } }################# end tryLocal
But of course if you really need to parse parameter strings then you should look at CGI or a similar module. Parsing parameter strings is not as strightforward as this code suggests.#!/usr/bin/perl use strict; use warnings; sub tryLocal{ my $param_str=shift; my @pairs= split(/\&/,$param_str); my %params; foreach my $pair (@pairs) { my ($key,$value)= =split(/\=/,$pair); $params{$key}=$value; } return %params } my %params=tryLocal("name=james&color=red&age=12");
This works.
Well, insofar as it sort of does what you want it to do, you are correct. But it doesnt run under strict. And what happens if I come along and add some code:
Oops. So now you need to localize everything... But why bother, why not declare all of these variables as lexicals and pass them around _explicitly_ instead of _implicitly_ with all the dangers it carries?@in=qw(foo bar baz); tryLocal("name=james&color=red&age=12"); print "$name : @in\n";
Which leads me to say. I was wrong. As you fall in love with local (which i have been doing all summer), you must abandon my old ways. Create your hash and check $TEMP{$passcode} instead of $passcode.
Im not really sure what all this means. The simplest thing that you can do to improve your programming skills is to turn strict on. That will force you to abandon all of your old ways and replace them with better ones, and eventually learn when the old ways were actually the right way, along with why most of the time why the old ways were the wrong way.
A little tip. This is one of the best places to learn quality perl. Hang out listen and learn, ask questions and such. But dont try to tell us that our hard learned lessons are BS. At least not until you know enough to not use phrases like main root hash environment.
:-)
--- demerphq
don't use symrefs until you understand why you shouldn't use symrefs
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Strict, my and warnings
by true (Pilgrim) on Oct 15, 2002 at 21:53 UTC | |
by Aristotle (Chancellor) on Oct 16, 2002 at 11:39 UTC | |
by demerphq (Chancellor) on Oct 16, 2002 at 19:23 UTC | |
by Aristotle (Chancellor) on Oct 17, 2002 at 00:30 UTC | |
by true (Pilgrim) on Oct 16, 2002 at 11:55 UTC | |
by Aristotle (Chancellor) on Oct 16, 2002 at 12:04 UTC | |
by demerphq (Chancellor) on Oct 16, 2002 at 11:13 UTC | |
by true (Pilgrim) on Oct 16, 2002 at 12:21 UTC | |
by demerphq (Chancellor) on Oct 16, 2002 at 14:17 UTC |