Register a SA Forums Account here!
JOINING THE SA FORUMS WILL REMOVE THIS BIG AD, THE ANNOYING UNDERLINED ADS, AND STUPID INTERSTITIAL ADS!!!

You can: log in, read the tech support FAQ, or request your lost password. This dumb message (and those ads) will appear on every screen until you register! Get rid of this crap by registering your own SA Forums Account and joining roughly 150,000 Goons, for the one-time price of $9.95! We charge money because it costs us money per month for bills, and since we don't believe in showing ads to our users, we try to make the money back through forum registrations.
 
  • Locked thread
Mithaldu
Sep 25, 2007

Let's cuddle. :3:
Just in case neither of you guys has seen it yet, german perl hacker elmex has recently made his own minecraft-inspired OpenGL game in Perl public. It works on both linux, windows and mac and for windows there are binary packages available:

http://ue.o---o.eu

https://www.youtube.com/watch?v=iZ4Qyto-JzI
https://www.youtube.com/watch?v=qd95SrdNkgE
https://www.youtube.com/watch?v=5SxD5PabLFQ

Adbot
ADBOT LOVES YOU

I like turtles
Aug 6, 2009

A quick question, I hope.
You have two modules, ABC::EFG and FOO::BAR, each containing an arbitrary function happy().
As I understand it, you can do the following:
code:
use ABC::EFG;
happy();
And it would run fine, calling the happy function from the declared module.
What happens if you do this?
code:
use ABC::EFG;
use FOO::BAR;
happy();
Do you get an error? Do you get the happy function from the last declared module, in this case FOO::BAR?
code:
use ABC::EFG;
use FOO::BAR;
ABC::EFG.happy();
This should work to call the happy function from ABC::EFG, right? Is that the only way around it?

MacGowans Teeth
Aug 13, 2003

I like turtles posted:

A quick question, I hope.
You have two modules, ABC::EFG and FOO::BAR, each containing an arbitrary function happy().
As I understand it, you can do the following:
code:
use ABC::EFG;
happy();
And it would run fine, calling the happy function from the declared module.
What happens if you do this?
code:
use ABC::EFG;
use FOO::BAR;
happy();
Do you get an error? Do you get the happy function from the last declared module, in this case FOO::BAR?
code:
use ABC::EFG;
use FOO::BAR;
ABC::EFG.happy();
This should work to call the happy function from ABC::EFG, right? Is that the only way around it?
The way you have it written there, you'd only be able to call it if the package exports that function to the main namespace using Exporter. Otherwise you'd have to use the qualified name ABC::EFG::happy(). If both packages exported a function with the same name, it would be the same thing as creating two functions with the same name in the main namespace, so you should get a compilation error, I think.

Mario Incandenza
Aug 24, 2000

Tell me, small fry, have you ever heard of the golden Triumph Forks?
The most recent sub imported with a use statement will be the one that's called. No errors or warnings will be emitted by Exporter:

code:
  *{"$callpkg\::$_"} = \&{"$pkg\::$_"} foreach @_; 
After a bunch of other stuff, it just straight up clones of a list of named subs from their typeglob in one package to the caller.

Perl does have a 'subroutine undefined' warning but it only shows up when the parser (compile-time or run-time) encounters a named sub block that matches an existing one in the current package:

code:
perl> sub foo { 'foo' }
perl> sub foo { 'bar' }
Subroutine foo redefined at (eval 68) line 1.

Back Stabber
Feb 5, 2009
Another noob question for you guys.
Trying to replace all letters with their respective numbers (A to 1, B to 2, etc), I come up with this:
code:
$letters =~s/[A-Z]/[1-26]/g;
Which would turn any sentence into something looking like this:
code:
[1-26][1-26][1-26][1-26][1-26][1-26][1-26][1-26][1-26]
I know that it interprets the [A-Z] as all letters, but I would like it to interpret the numbers in the same way that "=~ s/[a-z]/[A-Z]/g;" is interpreted for uppercasing.

Is there any way to make this replacement without a bunch of if and elsif statements? Any help is appreciated.

Another quick question, I see the words "foo bar" pop up in every textbook / example I run into (online and off), is it like the Wilhelm scream for perl programmers?

TiltedAtWindmills
Sep 4, 2009

Back Stabber posted:

Another noob question for you guys.
Trying to replace all letters with their respective numbers (A to 1, B to 2, etc), I come up with this:
code:
$letters =~s/[A-Z]/[1-26]/g;
Which would turn any sentence into something looking like this:
code:
[1-26][1-26][1-26][1-26][1-26][1-26][1-26][1-26][1-26]
I know that it interprets the [A-Z] as all letters, but I would like it to interpret the numbers in the same way that "=~ s/[a-z]/[A-Z]/g;" is interpreted for uppercasing.

Is there any way to make this replacement without a bunch of if and elsif statements? Any help is appreciated.

Another quick question, I see the words "foo bar" pop up in every textbook / example I run into (online and off), is it like the Wilhelm scream for perl programmers?

Sounds like you want tr///. (Scroll down a bit.)

Anaconda Rifle
Mar 23, 2007

Yam Slacker

Back Stabber posted:

Another noob question for you guys.
Trying to replace all letters with their respective numbers (A to 1, B to 2, etc), I come up with this:
code:
$letters =~s/[A-Z]/[1-26]/g;
Which would turn any sentence into something looking like this:
code:
[1-26][1-26][1-26][1-26][1-26][1-26][1-26][1-26][1-26]
I know that it interprets the [A-Z] as all letters, but I would like it to interpret the numbers in the same way that "=~ s/[a-z]/[A-Z]/g;" is interpreted for uppercasing.

Is there any way to make this replacement without a bunch of if and elsif statements? Any help is appreciated.

If you want just upper case letters changed to 1..26, use this:
code:
=~ s/([A-Z])/ord($1) - 64/eg
If you want all letters, use this:
code:
=~ s/([a-zA-Z])/ord(uc $1) - 64/eg

Back Stabber posted:

Another quick question, I see the words "foo bar" pop up in every textbook / example I run into (online and off), is it like the Wilhelm scream for perl programmers?

http://en.wikipedia.org/wiki/Metasyntactic_variable

qntm
Jun 17, 2009

Anaconda Rifle posted:

If you want just upper case letters changed to 1..26, use this:
code:
=~ s/([A-Z])/ord($1) - 64/eg
If you want all letters, use this:
code:
=~ s/([a-zA-Z])/ord(uc $1) - 64/eg

Oh sure, assuming your character set is ASCII and the upper- and lower-case letters form contiguous ranges...

Anaconda Rifle
Mar 23, 2007

Yam Slacker

qntm posted:

Oh sure, assuming your character set is ASCII and the upper- and lower-case letters form contiguous ranges...

Not to mention that the output is ambiguous as gently caress. Was the original string "Z", "BF", "2F", "B6", or "26"? I gave the self-proclaimed noob an answer that may lead to him/her reading up on ord and the e modifier on the substitution. My work here is done. *Flies away on an unnecessary look-behind assertion*

baquerd
Jul 2, 2007

by FactsAreUseless

qntm posted:

the upper- and lower-case letters form contiguous ranges...

There are character sets where this is not true?

tef
May 30, 2004

-> some l-system crap ->

baquerd posted:

There are character sets where this is not true?

unicode



also it is locale dependent

Filburt Shellbach
Nov 6, 2007

Apni tackat say tujay aaj mitta juu gaa!
more like ebcdic

Mithaldu
Sep 25, 2007

Let's cuddle. :3:
I think this deserves a mention here too: If you use catalyst and Template::Toolkit you can fix XSS issues globally now: http://blogs.perl.org/users/mithaldu/2011/07/fixing-xss-in-catalyst-with-a-really-big-hammer.html

qntm
Jun 17, 2009

baquerd posted:

There are character sets where this is not true?

Yes.

Filburt Shellbach
Nov 6, 2007

Apni tackat say tujay aaj mitta juu gaa!

Mithaldu posted:

I think this deserves a mention here too: If you use catalyst and Template::Toolkit you can fix XSS issues globally now: http://blogs.perl.org/users/mithaldu/2011/07/fixing-xss-in-catalyst-with-a-really-big-hammer.html

Wow! I didn't know TT did no escaping by default. This makes me glad to be a Mason user, since it assumes HTML escaping unless otherwise specified.

uG
Apr 23, 2003

by Ralp

Mithaldu posted:

I think this deserves a mention here too: If you use catalyst and Template::Toolkit you can fix XSS issues globally now: http://blogs.perl.org/users/mithaldu/2011/07/fixing-xss-in-catalyst-with-a-really-big-hammer.html
Why the hell did this take so long

Mithaldu
Sep 25, 2007

Let's cuddle. :3:

Filburt Shellbach posted:

Wow! I didn't know TT did no escaping by default. This makes me glad to be a Mason user, since it assumes HTML escaping unless otherwise specified.
Eh, i could never get into Mason, it looks so PHPy. :/

uG posted:

Why the hell did this take so long
Because the original maintainer of TT has basically abandoned it and its internal structure is such that it requires a bit of cleverness to do this from the outside. There was another attempt with Template::HTML, but it used too much magic imo.

uG
Apr 23, 2003

by Ralp
I'm confused on how this piece of code is acting:
code:
# $drive = <dd class="odd">South Carolina kicked off, O. McCalebb returned kickoff for 19 yards</dd>

while($drive =~ m!<dd.*?>(.*?)</dd>!gs) {
     #do stuff
}
Anyone know why it keeps iterating back to the first match and never breaks out of the loop?

qntm
Jun 17, 2009

uG posted:

I'm confused on how this piece of code is acting:
code:
# $drive = <dd class="odd">South Carolina kicked off, O. McCalebb returned kickoff for 19 yards</dd>

while($drive =~ m!<dd.*?>(.*?)</dd>!gs) {
     #do stuff
}
Anyone know why it keeps iterating back to the first match and never breaks out of the loop?

Because m// doesn't modify $drive. It's still the same string, so it still matches.

uG
Apr 23, 2003

by Ralp

qntm posted:

Because m// doesn't modify $drive. It's still the same string, so it still matches.
That's not necessary.

Erasmus Darwin
Mar 6, 2001

uG posted:

Anyone know why it keeps iterating back to the first match and never breaks out of the loop?

Works for me. All I did was uncomment the first line (and wrap the string with single quotes) and change the loop body to print "$1\n";. It outputs the match and exits.

Vulture Culture
Jul 14, 2003

I was never enjoying it. I only eat it for the nutrients.
Perl, the language where there are 100 libraries for parsing SGML derivatives and everyone uses regular expressions anyway

Dinty Moore
Apr 26, 2007
Is there a way I can do this that will keep Perl 5.8.x from puking? I'm trying to conditionally use the right regex for the Perl version in use, but it seems the interpreter parses the regex anyway, causing Perl to lose its lunch about my using (?|...) syntax. Trying to put it in an eval { }; block doesn't work either, the parser still looks at it and gets cranky.

code:
our $url_rx;
if ($] >= 5.010) {
    $url_rx = qr{^
                  (afps?):/             # protocol specific prefix
                  (at)?/                # optionally specify atalk transport
                  (?:                   # authentication info block
                      ([^:\@/;]*)       # capture username
                      (?:;AUTH=([^:\@/;]+))? # capture uam name
                      (?::([^:\@/;]*))? # capture password
                  \@)?                  # closure of auth info capture
                  (?|([^:/\[\]:]+)|\[([^\]]+)\]) # capture target host
                  (?::([^:/;]+))?       # capture optional port
                  (?:/(?:               # start path capture
                      ([^:/;]+)         # first path element is vol name
                      (/.*)?            # rest of path is local subpath
                  )?)?                  # closure of path capture
                  $}xs;
}
elsif ($] >= 5.008) {
    # Since we can't do (?|...) in Perl 5.8.x (didn't get added till 5.10),
    # just leave it out in this version.
    $url_rx = qr{^
                  (afps?):/             # protocol specific prefix
                  (at)?/                # optionally specify atalk transport
                  (?:                   # authentication info block
                      ([^:\@/;]*)       # capture username
                      (?:;AUTH=([^:\@/;]+))? # capture uam name
                      (?::([^:\@/;]*))? # capture password
                  \@)?                  # closure of auth info capture
                  ([^:/\[\]:]+)         # capture target host
                  (?::([^:/;]+))?       # capture optional port
                  (?:/(?:               # start path capture
                      ([^:/;]+)         # first path element is vol name
                      (/.*)?            # rest of path is local subpath
                  )?)?                  # closure of path capture
                  $}xs;
}

slipped
Jul 12, 2001

Dinty Moore posted:

Is there a way I can do this that will keep Perl 5.8.x from puking? I'm trying to conditionally use the right regex for the Perl version in use, but it seems the interpreter parses the regex anyway, causing Perl to lose its lunch about my using (?|...) syntax. Trying to put it in an eval { }; block doesn't work either, the parser still looks at it and gets cranky.

code:
our $url_rx;
if ($] >= 5.010) {
    $url_rx = qr{^
                  (afps?):/             # protocol specific prefix
                  (at)?/                # optionally specify atalk transport
                  (?:                   # authentication info block
                      ([^:\@/;]*)       # capture username
                      (?:;AUTH=([^:\@/;]+))? # capture uam name
                      (?::([^:\@/;]*))? # capture password
                  \@)?                  # closure of auth info capture
                  (?|([^:/\[\]:]+)|\[([^\]]+)\]) # capture target host
                  (?::([^:/;]+))?       # capture optional port
                  (?:/(?:               # start path capture
                      ([^:/;]+)         # first path element is vol name
                      (/.*)?            # rest of path is local subpath
                  )?)?                  # closure of path capture
                  $}xs;
}
elsif ($] >= 5.008) {
    # Since we can't do (?|...) in Perl 5.8.x (didn't get added till 5.10),
    # just leave it out in this version.
    $url_rx = qr{^
                  (afps?):/             # protocol specific prefix
                  (at)?/                # optionally specify atalk transport
                  (?:                   # authentication info block
                      ([^:\@/;]*)       # capture username
                      (?:;AUTH=([^:\@/;]+))? # capture uam name
                      (?::([^:\@/;]*))? # capture password
                  \@)?                  # closure of auth info capture
                  ([^:/\[\]:]+)         # capture target host
                  (?::([^:/;]+))?       # capture optional port
                  (?:/(?:               # start path capture
                      ([^:/;]+)         # first path element is vol name
                      (/.*)?            # rest of path is local subpath
                  )?)?                  # closure of path capture
                  $}xs;
}
qr forces a compile, and eval { BLOCK } is also a compile time compile.

$url_rx = eval "qr//"; as a string will get you what you want

Dinty Moore
Apr 26, 2007

slipped posted:

qr forces a compile, and eval { BLOCK } is also a compile time compile.

$url_rx = eval "qr//"; as a string will get you what you want

Oh word. Looks like that solves my problem. Thanks.

homercles
Feb 14, 2010

shift / @_ question here. I ran into some unexpected behavior. Here is test code:
code:
sub print_args { print "[@_]\n"; }

sub call_bad  { shift->{cb}->(@_) }
sub call_good { my $self = shift; $self->{cb}->(@_) }

my $hash_glob = { cb => \&print_args };
my $hash_anon = { cb => sub { print_args(@_) } };
print "1. I expect [123], I got "; call_bad($hash_glob, 123);
print "2. I expect [123], I got "; call_bad($hash_anon, 123);

bless $hash_glob; bless $hash_anon;
print "3. I expect [123], I got "; $hash_glob->call_bad(123);
print "4. I expect [123], I got "; $hash_anon->call_bad(123);
print "5. I expect [123], I got "; $hash_glob->call_good(123);
print "6. I expect [123], I got "; $hash_anon->call_good(123);
Output is:
code:
1. I expect [123], I got [HASH(0x2397f40) 123]
2. I expect [123], I got [HASH(0x23b3c10) 123]
3. I expect [123], I got [main=HASH(0x2397f40) 123]
4. I expect [123], I got [main=HASH(0x23b3c10) 123]
5. I expect [123], I got [123]
6. I expect [123], I got [123]
My only guess is that in &call_bad, @_ seems to be evaluating before shift() occurs, even though to access the coderef that takes @_ the shift has to be done.

qntm
Jun 17, 2009
I'm hoping to put some snippets of Perl code online. Is there a Javascript library which I can use to apply syntax highlighting to my code? I'm hoping to be able to write <pre class="perl">...perl code...</pre> and have the Javascript prettify it when the user loads the page (because hell if I'm going to drag all my code through a manual prettifier at my end).

Anaconda Rifle
Mar 23, 2007

Yam Slacker

qntm posted:

I'm hoping to put some snippets of Perl code online. Is there a Javascript library which I can use to apply syntax highlighting to my code? I'm hoping to be able to write <pre class="perl">...perl code...</pre> and have the Javascript prettify it when the user loads the page (because hell if I'm going to drag all my code through a manual prettifier at my end).

pastebin does all types of syntax highlighting. I'm not sure if you're putting your code just anywhere and linking to it, or you are going to put it in your personal space.

qntm
Jun 17, 2009
I'm putting it on my site, so I'd like a JS library I can just put there.

Anaconda Rifle
Mar 23, 2007

Yam Slacker
I found this person's site: http://www.tohtml.com/. Maybe you can ask them how they did it.

Vulture Culture
Jul 14, 2003

I was never enjoying it. I only eat it for the nutrients.
GeSHi is the one I use on my site. It doesn't do Erlang, but it handles every other language I've thrown at it.

clockwork automaton
May 2, 2007

You've probably never heard of them.

Fun Shoe
I use Google Code Prettify on my site. It has some nice default supported themes and can handle most languages.

vomit-orchestra
Aug 6, 2008

Don't be ridiculous. We're flamingos. And good ones.
Writing my first script :3:
however I have a question about how to cut down the output to what I need/want.
A little background, I've got a few netapp's that I'd like to keep an eye on the temperature and eventually add it to RRD.

code:
#!/bin/bash
SERVER="server01"
USR="root"
OUT="out.txt"
ssh $USR@$SERVER  environment status chassis list-sensors > $OUT
exit 0
this gives me the output of:
code:

Sensor Name              State          Current    Critical     Warning     Warning    Critical
                                        Reading       Low         Low         High       High
-------------------------------------------------------------------------------------------------
LCD board Temperature    normal            21 C         0 C        10 C        42 C        52 C
MB Front Zone Temperaturenormal            26 C         0 C        10 C        49 C        58 C
MB Rear Zone Temperature normal            29 C         0 C        10 C        50 C        59 C
PSU1 Present             normal           good
PSU1 AC IN               normal           good
PSU1 12V                 normal           good
PSU1 5V                  normal           good
PSU1 FAN1                normal          3726 RPM    1800 RPM    1850 RPM      --          --
PSU1 FAN2                normal          3459 RPM    1800 RPM    1850 RPM      --          --
PSU2 Present             normal           good
PSU2 AC IN               normal           good
PSU2 12V                 normal           good
PSU2 5V                  normal           good
PSU2 FAN1                normal          3734 RPM    1800 RPM    1850 RPM      --          --
PSU2 FAN2                normal          3308 RPM    1800 RPM    1850 RPM      --          --
CPU 3.3V Active          normal          3356 mV       --        3135 mV     3465 mV       --
CPU 3.3V Standby         normal          3356 mV       --        3135 mV     3465 mV       --
CPU 5V                   normal          5169 mV       --        4750 mV     5250 mV       --
CPU 12V                  normal         11875 mV       --       11400 mV    12600 mV       --
SYS_FAN_1 Fan1           normal          2366 RPM    1200 RPM    1250 RPM      --          --
SYS_FAN_1 Fan2           normal          2210 RPM    1200 RPM    1250 RPM      --          --
SYS_FAN_2 Fan1           normal          2343 RPM    1200 RPM    1250 RPM      --          --
SYS_FAN_2 Fan2           normal          2260 RPM    1200 RPM    1250 RPM      --          --
NVRAM6-temperature-3     normal            28 C         0 C        10 C        50 C        64 C
NVRAM6-battery-3         normal          4032 mV     3649 mV     3849 mV     4250 mV     4250 mV
Essentially, I want to cut everything else out except the temperature related categories. Would a find/grep/cut be the best option for this? Any help is appreciated

--
nevermind, I am an idiot :p

code:
#!/bin/bash
SERVER="server01"
USR="root"
ssh $USR@$SERVER  environment status chassis list-sensors | grep "LCD board Temperature" | awk '{print $5}'

vomit-orchestra fucked around with this message at 07:29 on Jul 28, 2011

uG
Apr 23, 2003

by Ralp
Is there a module that can take a variable and return what was used to set its value? So if earlier in the script you have $foo = 1 + 2, I could do something like how($foo) and have it return a string with a value of '1 + 2' instead of 3?

Vulture Culture
Jul 14, 2003

I was never enjoying it. I only eat it for the nutrients.

uG posted:

Is there a module that can take a variable and return what was used to set its value? So if earlier in the script you have $foo = 1 + 2, I could do something like how($foo) and have it return a string with a value of '1 + 2' instead of 3?
Perl isn't quantum mechanics :(

There are a number of debugging tools you can use, but it sounds like you want to set a watch on this variable. There's a number of tools to do that. This is one:

http://docs.activestate.com/activeperl/5.8/lib/Tie/Watch.html

qntm
Jun 17, 2009

uG posted:

Is there a module that can take a variable and return what was used to set its value? So if earlier in the script you have $foo = 1 + 2, I could do something like how($foo) and have it return a string with a value of '1 + 2' instead of 3?

That sounds like it would be a pretty magical feature if it existed in any language. May I ask why you want this? I'm really interested to know what the applications could be.

uG
Apr 23, 2003

by Ralp

qntm posted:

That sounds like it would be a pretty magical feature if it existed in any language. May I ask why you want this? I'm really interested to know what the applications could be.
I want to display the equation used to calculate certain values next to the actual values. So right now this involves:
code:
my %foo.
$foo{result} = $bar + 2;
$foo{show} = $bar . ' + 2';

print $foo{show} . '=' . $foo{result};
Except i'm working with somebodies old script, so its a pretty tedious process.

qntm
Jun 17, 2009

uG posted:

I want to display the equation used to calculate certain values next to the actual values. So right now this involves:
code:
my %foo.
$foo{result} = $bar + 2;
$foo{show} = $bar . ' + 2';

print $foo{show} . '=' . $foo{result};
Except i'm working with somebodies old script, so its a pretty tedious process.

Perhaps something like

code:
my $expression = "2 + 2";
my $result = eval $expression;

print $equation, " = ", $result;
?

uG
Apr 23, 2003

by Ralp

qntm posted:

Perhaps something like

code:
my $expression = "2 + 2";
my $result = eval $expression;

print $equation, " = ", $result;
?
Well the problem is going through this huge lovely script and changing everything to work like that. I was hoping there was something that would 'just work' by creating a duplicate of any variable that is created except that it would stringify it.

Adbot
ADBOT LOVES YOU

TasteMyHouse
Dec 21, 2006
you could write something that parses the script, grabbing all the strings on the right hand side of assignments to the variables you care about. It actually probably wouldn't be that hard.

  • Locked thread