Beefy Boxes and Bandwidth Generously Provided by pair Networks
XP is just a number
 
PerlMonks  

Re: Script stopped working...

by poj (Abbot)
on Mar 18, 2018 at 15:18 UTC ( [id://1211190]=note: print w/replies, xml ) Need Help??


in reply to Script stopped working...

It looks very likely hippo's solution will solve your GLOB error but there are 2 further issues to be aware of. When you add use strict you may encounter the error

Can't use an undefined value as an ARRAY reference at ...

on this line.

my $size = @{$data->{User}};

This might occur on the last request when no more records are in the response. A fix would be

my $size = @{$data->{User} || []};

The other error with XML::Simple is more subtle and will only appear it there is one record in the response. In this case the error would be

Not an ARRAY reference at ...

The fix is to use ForceArray on the User element

my $xml = new XML::Simple(ForceArray => ['User']);

Example with strict

#!/usr/bin/perl use strict; use warnings; use LWP::UserAgent; use LWP::Protocol::https; use XML::Simple; use Time::Piece; my $ua = LWP::UserAgent->new(ssl_opts => { SSL_verify_mode => IO::Socket::SSL::SSL_VERIFY_NONE, verify_hostname => 0, }); my $xml = new XML::Simple(ForceArray => ['User']); # Get Unity subscriber data from cucxnpub one page at a time # 2000 records to a page my $url = "https://cucxnpub/vmrest/users?rowsPerPage=2000"; my $userName = 'user'; my $passwd = 'password'; my $page = 1; my @userdata; while(1){ my $req = HTTP::Request->new(GET => $url."&pageNumber=$page"); $req->authorization_basic($userName,$passwd); $req->content_type('application/xml'); my $response = $ua->request($req); unless ( $response->is_success ){ die $response->status_line; } my $content = $response->decoded_content; my $data = $xml->XMLin($content); my $size = @{ $data->{User} || [] }; print "Page $page : $size\n"; # If we don't get at least one user end the loop last if ( $size == 0 ); # Build the userdata array, each entry contains "username,extension" for my $user ( @{$data->{User}} ){ push @userdata,"$user->{Alias},$user->{DtmfAccessId}"; } ++$page; } # Create a timestamp containing year, month, and day my $timestamp = localtime->ymd(''); # Dump the results of the unity query to a file my $dir = '/usr/scripts/unityLDAP/'; my $outfile = $dir."$timestamp-unity.csv"; open UNITY,'>',$outfile or die "Could not open $outfile : $!"; print UNITY "$_\n" for @userdata; close UNITY; printf "%d records written to %s\n",scalar @userdata,$outfile;
poj

Replies are listed 'Best First'.
Re^2: Script stopped working...
by gentoobob (Novice) on Mar 18, 2018 at 17:26 UTC

    Ok great! I will def look into that. Thank you for your response. I greatly appreciate it.

      From various replies:

      Ive been trying to fix this script for 2 weeks now and putting serious time into.
      I greatly appreciate it.
      Hmm... If only there were some way to show your appreciation...   (hint, hint...)


      Give a man a fish:  <%-{-{-{-<

        haha I donate to a few open source foundations, one being FreeBSD, the other is just random. So you got it. I will "show my appreciation" at donation time. thank you again!

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://1211190]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others taking refuge in the Monastery: (5)
As of 2024-04-16 15:09 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found