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
Hughmoris
Apr 21, 2007
Let's go to the abyss!
What is a non-clunky way to generate an SSN? I have a working solution but I'm thinking there is a more elegant solution...
Perl code:
	$ssn_first = sprintf("%03d", int(rand(999)));
	$ssn_middle = sprintf("%02d", int(rand(99)));
	$ssn_last = sprintf("%03d", int(rand(999)));
	$ssn = "$ssn_first-$ssn_middle-$ssn_last";
My new way is to generate a random 9-digit number but I can't find how to splice it and insert the '-' characters. Python has spoiled me in this regard.

*Hot drat, I'm really enjoying learning the powers of regular expressions. I created a new solution, not sure if its better or worse:
Perl code:
my $ssn = sprintf("%09d", int(rand(999999999)));
$ssn =~ s/(\d{3})(\d{2})(\d{4})/$1-$2-$3/;
print $ssn;  #outputs a formatted number 123-45-6789

Hughmoris fucked around with this message at 05:21 on Mar 30, 2015

Adbot
ADBOT LOVES YOU

het
Nov 14, 2002

A dark black past
is my most valued
possession
Depending on how much you care about performance, probably faster to do something like $ssn = substr($ssn, 0, 3) . "-" . substr($ssn, 3, 2) . "-" . substr($ssn, 5, 4); (clunky compared to python's string slices, admittedly)

Toshimo
Aug 23, 2012

He's outta line...

But he's right!

Hughmoris posted:

What is a non-clunky way to generate an SSN? I have a working solution but I'm thinking there is a more elegant solution...
Perl code:
	$ssn_first = sprintf("%03d", int(rand(999)));
	$ssn_middle = sprintf("%02d", int(rand(99)));
	$ssn_last = sprintf("%03d", int(rand(999)));
	$ssn = "$ssn_first-$ssn_middle-$ssn_last";
My new way is to generate a random 9-digit number but I can't find how to splice it and insert the '-' characters. Python has spoiled me in this regard.

If someone else wants to handle a technical implementation of what you've written, by all means.

But a valid US SSN has additional criteria beyond just being a series of 9 numbers that you'd probably want to take into account:

  • No SSN can have all 0's in any field. So, 000-XX-XXXX, XXX-00-XXXX, and XXX-XX-0000 are all invalid.
  • No SSN will ever be issued in the ranges 000-XX-XXXX, 666-XX-XXXX, or 900-XX-XXXX thru 999-XX-XXXX.
  • The following list of numbers are specifically invalid:

code:
 022-28-1852 141-18-6941 212-09-7694

 042-10-3580 165-16-7999 219-09-9998

 062-36-0794 165-18-7999 306-30-2348

 078-05-1120 165-20-7999 308-12-5070

 095-07-3645 165-22-7999 468-28-8779

 128-03-6045 165-24-7999 549-24-1889

 135-01-6629 189-09-2294 987-65-4320 
Whether or not you want to work any of that into your plans is up to you.

Hughmoris
Apr 21, 2007
Let's go to the abyss!

het posted:

Depending on how much you care about performance, probably faster to do something like $ssn = substr($ssn, 0, 3) . "-" . substr($ssn, 3, 2) . "-" . substr($ssn, 5, 4); (clunky compared to python's string slices, admittedly)

Thanks, that's what I was shooting for originally but I couldn't quite figure it out.

Toshimo posted:

If someone else wants to handle a technical implementation of what you've written, by all means.

But a valid US SSN has additional criteria beyond just being a series of 9 numbers that you'd probably want to take into account:
...
...
...

Hmmm. I honestly never paid much attention to the rules and regulations behind generating an SSN, learned something new today. For the purposes of my small project, I'm being lazy and including everything. Thanks for that info though.

Hughmoris fucked around with this message at 06:01 on Mar 30, 2015

EVGA Longoria
Dec 25, 2005

Let's go exploring!

If you have CPAN access, https://metacpan.org/pod/Data::Fake will give you:

Perl code:
use Data::Fake::Core;
	my $ssn = fake_digits("###-##-####");

Hughmoris
Apr 21, 2007
Let's go to the abyss!

EVGA Longoria posted:

If you have CPAN access, https://metacpan.org/pod/Data::Fake will give you:

Perl code:
use Data::Fake::Core;
	my $ssn = fake_digits("###-##-####");

I didn't even think to look on CPAN for a data generator. I need to poke around there a little bit more.

Filburt Shellbach
Nov 6, 2007

Apni tackat say tujay aaj mitta juu gaa!
code:
my $ssn = "###-##-####" =~ s/#/int rand 10/ger;

Mario Incandenza
Aug 24, 2000

Tell me, small fry, have you ever heard of the golden Triumph Forks?

uG posted:

code:

    my $file = (-f $_[0] && $_[0] ~~ @exts) ? $_[0] : return;

This is how i'd do it
but please don't use smart-matching in production code, the feature is deprecated and also a bit too smart for its own good

Hughmoris
Apr 21, 2007
Let's go to the abyss!
I'm trying to understand subroutines in perl. I'm following a short tutorial on Perl Maven and I'm unable to replicate the lesson: http://perlmaven.com/subroutines-and-functions-in-perl

Here is what I have. Its supposed to take in a first and last name, then call the subroutine and print those names as an illustration of how parameters are handled. However, nothing is printing for me after I enter the two names.
Perl code:
#!perl
use strict;
use warnings;
use diagnostics;

my $first_name = prompt("First name: ");
my $last_name = prompt("Last name: ");

sub prompt{
	my ($text) = @_;
	print $text;

	my $answer = <STDIN>;
	chomp $answer;
	return $answer;
}
Am I making a simple mistake somewhere?

*Well poo poo, I think I misunderstood how the function works. I was thinking the function would print the names I entered but I guess it's printing the actual prompt ("First name: ) etc... I'm a dummy.

Hughmoris fucked around with this message at 04:14 on Apr 1, 2015

Hughmoris
Apr 21, 2007
Let's go to the abyss!
I'm fiddling around and trying to understand a little bit about APIs. For practice, I'm making a JSON call to rottentomatoes for any new dvd releases. Here is the returned dataset: http://pastebin.com/dEjuKAiZ

My problem is that I don't know how to extract useful information from that dataset. For instance, if I wanted to return all the "title" values.

Here is what I have so far from piecing together stackoverflow ideas. Any pointers?
Perl code:
use strict;
use warnings;
use diagnostics;
use LWP::Simple;
use JSON qw( decode_json );
use Data::Dumper;

my $url = "http://api.rottentomatoes.com/api/public/v1.0/lists/dvds/new_releases.json?apikey=...";

my $json = get($url)
	or die "can't retriever url";
	
my $decoded_json = decode_json($json);

#print Dumper $decoded_json;

print "Title: ", $decoded_json->{"movies"}{"title"};
That gives me an error saying its not a HASH reference.

EVGA Longoria
Dec 25, 2005

Let's go exploring!

Hughmoris posted:

I'm fiddling around and trying to understand a little bit about APIs. For practice, I'm making a JSON call to rottentomatoes for any new dvd releases. Here is the returned dataset: http://pastebin.com/dEjuKAiZ

My problem is that I don't know how to extract useful information from that dataset. For instance, if I wanted to return all the "title" values.

Here is what I have so far from piecing together stackoverflow ideas. Any pointers?
Perl code:
use strict;
use warnings;
use diagnostics;
use LWP::Simple;
use JSON qw( decode_json );
use Data::Dumper;

my $url = "http://api.rottentomatoes.com/api/public/v1.0/lists/dvds/new_releases.json?apikey=...";

my $json = get($url)
	or die "can't retriever url";
	
my $decoded_json = decode_json($json);

#print Dumper $decoded_json;

print "Title: ", $decoded_json->{"movies"}{"title"};
That gives me an error saying its not a HASH reference.

Movies is an array of movies. You'd need to loop through it.

Perl code:
use strict;
use warnings;
use diagnostics;
use LWP::Simple;
use JSON qw( decode_json );
use Data::Dumper;

my $url = "http://api.rottentomatoes.com/api/public/v1.0/lists/dvds/new_releases.json?apikey=...";

my $json = get($url)
	or die "can't retriever url";
	
my $decoded_json = decode_json($json);

my $movies = $decoded_json->{"movies"};

for my $movie ($movies) {
	print "Movie Title: " . $movie->{title};
}

Hughmoris
Apr 21, 2007
Let's go to the abyss!

EVGA Longoria posted:

Movies is an array of movies. You'd need to loop through it.

Perl code:
use strict;
use warnings;
use diagnostics;
use LWP::Simple;
use JSON qw( decode_json );
use Data::Dumper;

my $url = "http://api.rottentomatoes.com/api/public/v1.0/lists/dvds/new_releases.json?apikey=...";

my $json = get($url)
	or die "can't retriever url";
	
my $decoded_json = decode_json($json);

my $movies = $decoded_json->{"movies"};

for my $movie ($movies) {
	print "Movie Title: " . $movie->{title};
}

Hmmm. I copied and ran your code and got an error "Not a hash reference at line 17" which is print "Movie Title: " . $movie->{title};

*Hopped on perl IRC and they helped me arrive to this solution, which will print out the title of each movie. I really need to sit down and try to understand how referencing and dereferencing works.
Perl code:
use warnings;
use diagnostics;
use LWP::Simple;
use JSON qw( decode_json );
use Data::Dumper;

my $url = "http://api.rottentomatoes.com/api/public/v1.0/lists/dvds/new_releases.json?apikey=...";

my $json = get($url)
    or die "can't retriever url";
    
my $decoded_json = decode_json($json);

print $_->{title}."\n" for @{$decoded_json->{movies}};

Hughmoris fucked around with this message at 05:46 on Apr 4, 2015

Hughmoris
Apr 21, 2007
Let's go to the abyss!

EVGA Longoria posted:

Movies is an array of movies. You'd need to loop through it.

Perl code:
use strict;
use warnings;
use diagnostics;
use LWP::Simple;
use JSON qw( decode_json );
use Data::Dumper;

my $url = "http://api.rottentomatoes.com/api/public/v1.0/lists/dvds/new_releases.json?apikey=...";

my $json = get($url)
	or die "can't retriever url";
	
my $decoded_json = decode_json($json);

my $movies = $decoded_json->{"movies"};

for my $movie ($movies) {
	print "Movie Title: " . $movie->{title};
}

After trying to understand referencing/dereferencing a little more (I still don't really get it), as an exercise I tried to figure out why the above code wouldn't work.

It's missing the array dereference at this line:
Perl code:
for my $movie ($movies) {
	print "Movie Title: " . $movie->{title};
}
When I change it to this, it runs great.
Perl code:
for my $movie (@$movies) {
	print "Movie Title: " . $movie->{title};
}

EVGA Longoria
Dec 25, 2005

Let's go exploring!

Hughmoris posted:

After trying to understand referencing/dereferencing a little more (I still don't really get it), as an exercise I tried to figure out why the above code wouldn't work.

It's missing the array dereference at this line:
Perl code:

for my $movie ($movies) {
	print "Movie Title: " . $movie->{title};
}

When I change it to this, it runs great.
Perl code:

for my $movie (@$movies) {
	print "Movie Title: " . $movie->{title};
}

What version of perl are you running? They've changed a bunch of things with referencing at various points, so it might be that.

Though I didn't test the code, so I might just be completely wrong.

Also, the code they wrote uses $_ which is a magic variable and is sometimes bad practice, since it can change easily.

EVGA Longoria fucked around with this message at 16:40 on Apr 4, 2015

Hughmoris
Apr 21, 2007
Let's go to the abyss!

EVGA Longoria posted:

What version of perl are you running? They've changed a bunch of things with referencing at various points, so it might be that.

Though I didn't test the code, so I might just be completely wrong.

Also, the code they wrote uses $_ which is a magic variable and is sometimes bad practice, since it can change easily.

I'm using Strawberry Perl, v5.20.1

Yeah I know I shouldn't use the $_ operator but it's just cool to see how much information can be extracted using such a small amount of code. :3:

Ellie Crabcakes
Feb 1, 2008

Stop emailing my boyfriend Gay Crungus

Hughmoris posted:

After trying to understand referencing/dereferencing a little more (I still don't really get it)
All a reference is is a pointer to a value. To access or manipulate that value and its members, you have to dereference, using the infix operator (->) or the appropriate sigil.

Hughmoris posted:

I'm using Strawberry Perl, v5.20.1
Is Perl on Windows any less miserable than I remember?

Hughmoris
Apr 21, 2007
Let's go to the abyss!

John Big Booty posted:

All a reference is is a pointer to a value. To access or manipulate that value and its members, you have to dereference, using the infix operator (->) or the appropriate sigil.
I've never took any CS courses or learned languages that used pointers so this is a bit new to me. I think I have the idea of referencing down pat but I start getting tripped up when I have nested dicts within arrays within dicts, and trying to get to the data I want. I just need to do some more readings and examples on the subject.


John Big Booty posted:

Is Perl on Windows any less miserable than I remember?

Its been working for me without any problems but I'm not really asking a lot of it, just basic learning/tutorials.

Hughmoris
Apr 21, 2007
Let's go to the abyss!
Is there a clean and simple way to format text tables?

Here is my output from grabbing the latest dvd releases and their release dates:
code:
Jinn(2014) 2015-04-12
A Most Violent Year(2015) 2015-04-07
The Immigrant(2014) 2015-04-07
The Voices(2015) 2015-04-07
Interstellar(2014) 2015-03-31
The Imitation Game(2014) 2015-03-31
Wild(2014) 2015-03-31
Island Of Lemurs: Madagascar(2014) 2015-03-31
Wild Card(2015) 2015-03-31
The Rewrite(2015) 2015-03-31
The Hobbit: The Battle of the Five Armies(2014) 2015-03-24
Unbroken(2014) 2015-03-24
Into the Woods(2014) 2015-03-23
Penguins Of Madagascar(2014) 2015-03-17
Exodus: Gods and Kings(2014) 2015-03-17
Annie(2014) 2015-03-17
I'd like to align the title and release date but everything I've found so far looks pretty drat complicated to accomplish it.

*Disregard, found something on CPAN (Text::Table) that makes it easy peasy.

code:
Title                                           DVD Release
Jinn(2014)                                      2015-04-12
A Most Violent Year(2015)                       2015-04-07
The Immigrant(2014)                             2015-04-07
The Voices(2015)                                2015-04-07
Interstellar(2014)                              2015-03-31
The Imitation Game(2014)                        2015-03-31
Wild(2014)                                      2015-03-31
Island Of Lemurs: Madagascar(2014)              2015-03-31
Wild Card(2015)                                 2015-03-31
The Rewrite(2015)                               2015-03-31
The Hobbit: The Battle of the Five Armies(2014) 2015-03-24
Unbroken(2014)                                  2015-03-24
Into the Woods(2014)                            2015-03-23
Penguins Of Madagascar(2014)                    2015-03-17
Exodus: Gods and Kings(2014)                    2015-03-17
Annie(2014)                                     2015-03-17

Hughmoris fucked around with this message at 03:53 on Apr 7, 2015

ephphatha
Dec 18, 2009




Is there a "say" equivalent that prints to STDERR? I've got a few scripts that I print warnings out to highlight bad data (so there's no need for line numbers) and remembering to put \n on the end of each string is a drag.

Ellie Crabcakes
Feb 1, 2008

Stop emailing my boyfriend Gay Crungus

Ephphatha posted:

Is there a "say" equivalent that prints to STDERR? I've got a few scripts that I print warnings out to highlight bad data (so there's no need for line numbers) and remembering to put \n on the end of each string is a drag.
say STDERR "bloobledebloop";

Hughmoris
Apr 21, 2007
Let's go to the abyss!
Does anyone have any recommended readings or tips for learning how to scrape a webpage with perl?

I'm wanting to scrape a local minor league baseball team's schedule for this year. It looks like the table that holds the dates is generated with javascript. The schedule is found here:http://www.milb.com/milb/stats/stats.jsp?t=t_sch&cid=4124&sid=t4124&stn=true

I'm googling for some ideas but they seem to be all over the place, including using selenium, phantomJS, Mojo::DOM and so on. Any tips on how to go about this?

*Hopped on IRC #perl and someone opened my eyes on how to use the network analyzer tool in Chrome. I found where the schedule data is stored, and I'm thinking I'll have a much easier time parsing this:
http://www.milb.com/gen/stats/jsdata/2015/clubs/t4124_sch.js?m=1428547560266

Hughmoris fucked around with this message at 04:09 on Apr 9, 2015

Ellie Crabcakes
Feb 1, 2008

Stop emailing my boyfriend Gay Crungus

This should definitely go in the Coding Horrors thread.

Sebbe
Feb 29, 2004

Hughmoris posted:

Does anyone have any recommended readings or tips for learning how to scrape a webpage with perl?

My personal go-tos for that are WWW::Mechanize and HTML::Query.

As an example, here's a recent thing I made, which scrapes some messages from a login-protected site to a JSON API.

Salt Fish
Sep 11, 2003

Cybernetic Crumb
Im bad but you could wget or curl and pipe the output into your script as standard in via a cron.

Ellie Crabcakes
Feb 1, 2008

Stop emailing my boyfriend Gay Crungus

Salt Fish posted:

Im bad but you could wget or curl and pipe the output into your script as standard in via a cron.
That's the easy part.

leedo
Nov 28, 2000

I'm a big fan of Web::Scraper

Hughmoris
Apr 21, 2007
Let's go to the abyss!

Sebbe posted:

My personal go-tos for that are WWW::Mechanize and HTML::Query.

As an example, here's a recent thing I made, which scrapes some messages from a login-protected site to a JSON API.

leedo posted:

I'm a big fan of Web::Scraper


Thanks for these. I've been poking around with Web::Scraper and having a little bit of luck

Hughmoris
Apr 21, 2007
Let's go to the abyss!
Using Perl in a Windows environment, is there a simple way to check if an application is open? In my script, I want to check if LotusNotes.exe is open before continuing. If it's not, I want to kill the script. Google is showing few examples and they seem a little beyond my understanding.

Roseo
Jun 1, 2000
Forum Veteran
https://metacpan.org/pod/P9Y::ProcessTable looks to be a cross platform implementation but for win32 ultimately wraps https://metacpan.org/pod/Win32::Process::Info and https://metacpan.org/pod/Win32::Process. I'd use the one that you grasp the api easiest on.

Hughmoris
Apr 21, 2007
Let's go to the abyss!

Roseo posted:

https://metacpan.org/pod/P9Y::ProcessTable looks to be a cross platform implementation but for win32 ultimately wraps https://metacpan.org/pod/Win32::Process::Info and https://metacpan.org/pod/Win32::Process. I'd use the one that you grasp the api easiest on.

Thanks for this.

RICHUNCLEPENNYBAGS
Dec 21, 2010

Hughmoris posted:

Using Perl in a Windows environment, is there a simple way to check if an application is open? In my script, I want to check if LotusNotes.exe is open before continuing. If it's not, I want to kill the script. Google is showing few examples and they seem a little beyond my understanding.

Using a Perl script to automate Lotus Notes. The 90s are back.

Hughmoris
Apr 21, 2007
Let's go to the abyss!

RICHUNCLEPENNYBAGS posted:

Using a Perl script to automate Lotus Notes. The 90s are back.

They never left. :smug:

I'm not a programmer, was just looking for the easiest way to go about monitoring a Lotus Notes inbox for a work project. Strawberry Perl happen to work right out of the box, and I found a Perl script from 2003 that I could tweak to my purposes. I had a working solution in about 30 minutes. Very cool stuff.

Hughmoris
Apr 21, 2007
Let's go to the abyss!
If I'm a dummy with little knowledge of Perl and no knowledge of web apps, is Dancer2 my easiest path to creating a simple web app?

Roseo
Jun 1, 2000
Forum Veteran

Hughmoris posted:

If I'm a dummy with little knowledge of Perl and no knowledge of web apps, is Dancer2 my easiest path to creating a simple web app?

Look at Dancer::Plugin::SimpleCRUD

http://advent.perldancer.org/2011/2

Hughmoris
Apr 21, 2007
Let's go to the abyss!

Roseo posted:

Look at Dancer::Plugin::SimpleCRUD

http://advent.perldancer.org/2011/2

Thanks. I don't know if I'll even get that far as I can't successfully install Dancer2. On ubuntu, using the command "CPAN install dancer2" will do some processes and eventually display "Running Build install
make test had returned bad status, won't install without force" before stopping the install.

I then tried "cpan force install dancer2" and "cpan fforce install dancer2" and they both ended with the same result and same error message. Not sure where to go from here.

Ellie Crabcakes
Feb 1, 2008

Stop emailing my boyfriend Gay Crungus

Hughmoris posted:

Thanks. I don't know if I'll even get that far as I can't successfully install Dancer2. On ubuntu, using the command "CPAN install dancer2" will do some processes and eventually display "Running Build install
make test had returned bad status, won't install without force" before stopping the install.

I then tried "cpan force install dancer2" and "cpan fforce install dancer2" and they both ended with the same result and same error message. Not sure where to go from here.
Which test failed?

Mithaldu
Sep 25, 2007

Let's cuddle. :3:
1 get on irc, the dancer people have a channel and love to help

2 to force install, launch the CPAN shell by calling cpan as it is, then run force install whatever

3 if tests fail on a cpan install, copy paste the whole output to gist.github.com or pastie.org, so people don't have to read your mind ;)

4 get on irc, seriously

(irccloud is a great free tool for that)

Hughmoris
Apr 21, 2007
Let's go to the abyss!

John Big Booty posted:

Which test failed?

Lots and lots.


Mithaldu posted:

1 get on irc, the dancer people have a channel and love to help

2 to force install, launch the CPAN shell by calling cpan as it is, then run force install whatever

3 if tests fail on a cpan install, copy paste the whole output to gist.github.com or pastie.org, so people don't have to read your mind ;)

4 get on irc, seriously

(irccloud is a great free tool for that)

I took your advice and hopped on perl IRC. Someone was nice enough to help me out. They had me install the perl module local::lib for reasons I don't fully understand but I'm up and running now. Just created my first "Hello world!" web app. :3:

Hughmoris fucked around with this message at 04:12 on May 13, 2015

Ellie Crabcakes
Feb 1, 2008

Stop emailing my boyfriend Gay Crungus

Hughmoris posted:

Lots and lots.
This is never the right answer.

Adbot
ADBOT LOVES YOU

Hughmoris
Apr 21, 2007
Let's go to the abyss!

John Big Booty posted:

This is never the right answer.

I'm pretty unfamiliar with how modules are installed but it seemed like everything failed. Unfortunately I didn't save a copy of the log before I fixed the problem so I can only give vague answers

  • Locked thread