Janek, et al -- ...and then Janek Schleicher said... % % David T-G wrote at Wed, 12 Jun 2002 15:39:41 +0200: % % > I'm trying to be good and so I use "my $variable" rather than making them global, and I prefer to ... % % Wait a moment. % It seems like that: % - You want to write Perl scripts.
Yep. % - You've heard that it is better to use use strict; use warnings; Yep; believe it, too. % - Now Perl's complaining about many no declared variables. % - You declare them all at the beginning. In this limited context, yes. I wouldn't usually declare them all up front. % - Now Perl gives warnings because there are some undefined variables. % - Now you want to assign all variables values like "", 0, () or so for the beginning. Well, not necessarily. I didn't really need my ($a, @b, %c) = "" ; [ignoring for the moment that that doesn't work] but that instead grew out of my original requirements. If you look at my ($artist, $album, $track) ; while (<>) { ($artist, $album, $track) = split(/\//); if ( $track eq "foo" ) ... } then you see that when a short path is read in (perhaps just album and track, thereby shifting things over a space) the script throws an error because $track is undef and shouldn't be used for comparison. *That* is where you get the "not just define but set, too" mentality of the script. I'll be pruning that back a bit when I next sit down to work on it, I assure you :-) % % I think, that it is my obligation to warn you % that use strict; use warnings; wasn't made for that way. Now, them's fightin' word, pardner! Into the street! % % It remembers me to a mafia killer % who has to swear to his mother to have a more christian life style. % So now, he always does a prayer before he kills anybody. % A more christian life style. *grin* % (Please don't take it too serious :-))) Oh, I'm not. Don't worry :-) % % use strict; use warnings; wasn't made only to make the Perl Community luckier. % It can help to avoid typical mistakes like % (a) mispelling of a variable % (b) using of an undefined variable (what in the most cases isn't wanted) Right; that, in fact, is what I have going on above. % (c) side effects without lexical assignments % (e.g. to say in a subroutine $i = 5; % should'nt influence the $i variable from the main program) Agreed. % - ... % - ... % % If the variables are declared at the beginning, % assigned with the standard values for undefined values. % you only can check (a). Hmmm... Well, I'll grant you that, but what about the example above? It would only be undef for the first N lines that are short, and once it gets loaded then it will get reset instead. It seems more sensible to set it to a defined but appropriate value for the upcoming loop than to integrate single-case undef logic into the loop, no? % % It's a very powerful concept to use variables only where they are needed. % It avoids many side effects. % If there are really some global variables needed, % declare them with the keyword 'our'. I still don't quite get the whole "our"/"local" think yet. Still workin' on that. % % (Another way to remove the ugly warnings from Perl % can be done with the CPAN pragma % use loose; :-) % ) I'll check that out; thanks. The idea, of course, is to write code that doesn't have warnings under any conditions :-) % % If there are many scalar values you need globally and % they belongs to a group, % like in the case of MP3-Data, % there's a good chance that a global hash can solve the problem. At the moment I'm storing then in a hash as $threez{$fullpath} = [($mp3,$source,$host,$artist,$disk,$track)] ; and will move that to a DB routine that will write to a hash, a flat file, a database, or whatnot -- and then I can pass it the parameters (where it will scope them locally) from this "read in some more data" subroutine, which will also scope them locally. Given all of that (*whew*), does it sound like I'm on the right track or instead still Not Getting It Even If TMTOWTDI? % % Cheerio, % Janek Thanks a *bunch* & HAND :-D -- David T-G * It's easier to fight for one's principles (play) [EMAIL PROTECTED] * than to live up to them. -- fortune cookie (work) [EMAIL PROTECTED] http://www.justpickone.org/davidtg/ Shpx gur Pbzzhavpngvbaf Qrprapl Npg!
msg25980/pgp00000.pgp
Description: PGP signature