
mhx
Enthusiast
Oct 23, 2001, 10:24 PM
Post #4 of 4
(15058 views)
|
Re: Combining variables and constants
[In reply to]
|
Can't Post
|
|
1) Why add the overhead of loading IO::File when I can just use open? My application is fairly memory intensive as it is so I need to avoid overhead as much as possible. I don't think it'll introduce too much overhead. However, you could use open as well.
2) How does Perl know that the colon following the string variable isn't part of the variable name? Put another way, how does it know to interpolate the value of $code and not $code:? In Perl, the colon isn't a valid character for an identifier. So Perl will always stop before the colon and use only $code. However, there's a problem if two colons follow a variable name, because then the name of the variable is treated as a namespace:
#!/bin/perl -w package code; $test = 'Hello'; package main; $code = 'Test'; print "$code::test\n"; This will print "Hello" and complain about $code being used only once. To achieve the (expected?) result, one has to write the following instead of the above: which will print "Test::test". I'm not sure about what exactly happens inside the perl interpreter if you put three colons after the identifier, as in your case, but to me it looks like it tries to access a variable in another namespace with the empty string as identifier. But that's pure speculation.
3) I simplified the example conversion file for the purpose of asking my main question. However, in reality, the values following the 3 colons could be a string which includes spaces, special characters etc. For example, it could look like: mycomment:::Perl's an awesome language! myemail:::me@me.com mycode::::this string begins with a colon. See the fourth colon over there? Isn't it pretty :) So, I was wondering if I should modify the regex to: /^$code:{3}(\w+)$/ # added a $ to the end It's even simpler. Just use the following regex:
Also note that I don't want the trailing carriage return, which I don't get when I use split(). The regex shouldn't capture the carriage return. If it does, your text file is in a different format than the native text files on the machine running the script normally are. If you want to explicitly disallow carriage returns and line feeds, use the following regex: This should get rid of all of them. -- Marcus
s$$ab21b8d15c3d97bd6317286d$;$"=547269736;split'i',join$,,map{chr(($*+= ($">>=1)&1?-hex:hex)+0140)}/./g;$"=chr$";s;.;\u$&;for@_[0,2];print"@_,"
|