Perl

25K

Solutions

10K

Contributors

Perl is a high-level, general-purpose, interpreted, dynamic programming languages with over 25 years of development. Perl 5 runs on over 100 platforms from portables to mainframes and is suitable for both rapid prototyping and large-scale development projects.  Perl gained widespread popularity as a Common Gateway Interface (CGI) scripting language, in part due to its regular expression and string parsing abilities. In addition to CGI, Perl is used for graphics programming, system administration, network programming, finance, bioinformatics, and other applications.

Share tech news, updates, or what's on your mind.

Sign up to Post

Checking the Alert Log in AWS RDS Oracle can be a pain through their user interface.  I made a script to download the Alert Log, look for errors, and email me the trace files.  In this article I'll describe what I did and share my script.
0
Get HTML5 Certified
LVL 9
Get HTML5 Certified

Want to be a web developer? You'll need to know HTML. Prepare for HTML5 certification by enrolling in July's Course of the Month! It's free for Premium Members, Team Accounts, and Qualified Experts.

In the distant past (last year) I hacked together a little toy that would allow a couple of Manager types to query, preview, and extract data from a number of MongoDB instances, to their tool of choice: Excel.

I primarily created the Web based tool to eliminate the  "Can you pull...." requests appearing in my inbox, and as Mac fanboys there weren't any toys around that would allow them to easily get at the data. When I say easily, the requirement was for a Manager proof point and click GUI, that could filter and flatten JSON / BSON documents with, multiple levels of nesting, into an Excel workbook. The nesting even rulled out the likes of iReports.

Anyway a day or two of hacking, while sat monitoring a training session (explains the quality of the code), and I had a rough little toy (Perl CGI + JS + Dojo Grid) that offered a fairly primitive query builder, that would allow them to: Browse, Filter, aggregate and Export data from a  MongoDB.

Had a quiet life for a bit, till a requirement to do something similar for exported JSON files, from a couple of the Social Media sites cropped up. Had a quick think, simply dropped 80% of the code in an evening, and came up with a little toy that would: Upload, Preview OR Convert a JSON document into an Excel Workbook. When I say JSON document, essentially a text file containing an anonymous JSON array of documents e.g.
[

Open in new window

0
 
LVL 26

Author Comment

by:arober11
Comment Utility
Possibly also of interest:

Java: Json to Excel
Python: StackOverflow thread
javaScript: jsFiddle - JSON to CSV Converter
Web based:  Json 2 HTML
0
I have been pestered over the years to produce and distribute regular data extracts, and often the request have explicitly requested the data be emailed as an Excel attachement; specifically Excel, as it appears: CSV files confuse (no Red or Green highlighting), Google Docs links confound (potentially require the recall and entry of a password) and Business Object, Crystal, iReports and the like all require some thought and action by the recipient (a non starter).

Anyway as the source of the data has generally been a remote Oracle / MySQL / DB2 / Mongo... DB, and the requirements generally included a statement mandating the extract be available first thing Monday morning ,the obvious action would be to write and schedule (crontab) a script to:

Connect to a DB
Submit a query or two to Extract some Data
Apply a bit of logic to reformat, add a few formula, filters, graphs and colour (Senior managers like Green)
Output the results as an Excel sheet
Encode and Email the sheet (as if you'd been in all weekend) to one ore more recipients.

...and all while you're still seeking your morning caffeine hit.

The above actions can be performed reasonably easily in any of the half dozen languages you'll likely find on the average (non-hardened) Unix box: Perl (Practical Extraction and Reporting Language), PHP, Java, Python......  but as the purpose of this script resembles one of the …
0
Sorting in PerlThere are many situations when we need to display the data in sorted order. For example: Student details by name or by rank or by total marks etc. If you are working on data driven based projects then you will use sorting techniques very frequently.
In Perl we have sort function which sorts a list alphabetically by default. But there is not the end. We need to sort:

   
an array numerically  or
     
case insensitive strings or
     
case sensitive strings
     
hash contents by keys or
   
hash contents by values or
   
reverse of all above said points

How sorting  works in Perl
Sort subroutine has three syntax and last one is the most used syntax.

    sort SUBNAME LIST
    sort BLOCK LIST
    sort LIST

In list context, it sorts the LIST and returns the sorted list value. In scalar context, the behavior of sort() is undefined.
If SUBNAME or BLOCK is omitted, sorts in standard string comparison order.

Standard string comparison means based on ASCII value of those characters. Like @arr = qw (Call all). In this case it will be sorted as Call all which was not expected. So to make it work properly we use case-insensitive sort.

 If SUBNAME is specified, it gives the name of a subroutine that returns an integer less than, equal to, or greater than 0 , depending on how the elements of the list are to be ordered. (The <=> and cmp operators are extremely useful in such routines.)

Note:
1
A year or so back I was asked to have a play with MongoDB; within half an hour I had downloaded,  installed and started the daemon, and had a console window open.

After an hour or two of playing at the command line I created a database or two, a couple of collections and a number of handcrafted JSON documents. At which point I went in search of a GUI and found RockMongo.

Another half hour of playing and I had a Web based GUI, that's great for ad-hoc queries and admin tasks, but your still to left to manually handcraft and enter your own JSON documents, via a textarea box. At which point I realised that if I was to evaluate the map-reduce functionality,  attempt to join data from two collections, let alone identify and evaluate any Business Reporting tools, that I would need to start cutting some code and convert an existing data source.

Data wasn't really an issue as the Companies business was data, so a simple choice  of either hooking up to a DB or using one or more of the many Excel reports kicking around and picking a language.  After a quick surf of the MongoDB site and read of the Perl tutorial I chose the latter and Perl.

After installing the necessary Perl libraries, enter:
cpan YAML Data::Dumper Spreadsheet::ParseExcel Tie::IxHash Encode Scalar::Util  JSON MongoDB MongoDB::OID File::Basename 

Open in new window

1
 
LVL 26

Author Comment

by:arober11
Comment Utility
If your looking for a relational DB / SQL equivalent then the following may possibly be of interest.
0
Email validation in proper way is  very important validation required in any web pages.
This code is self explainable except that Regular Expression which I used for pattern matching.

I originally published as a thread on my website : http://www.aliencoders.com/content/few-important-validations-using-regular-expression-perl and thought I would share it here within Experts Exchange.

First of all we should know the rules and regulation for using email address.
Email could be anything but it should have some boundary like
1. It will have @ and .
2. Username can be a mixture of character, digit and _ (usually) of any length but it should start with only character i.e from a-z or A-Z (I restrcited length from 8-15, yu can put your own).
3. Domain Name should be between 2 to 63
4. after the last . there should not be @ and it could be in range of 2-4 only like
aliencoders@mailing.commonworld is wrong

 ######################### 
# Author: Sanjeev Kumar Jaiswal     # 
# Date: 31 March 2010                      # 
# Purpose: Email Validation               # 
############################## 
 
#!C:/strawberry/perl/bin/perl.exe   #change shebang according to your path settings
use strict; 
use warnings; 
#  Taking Input from keyborad 
my $email = <STDIN>; 
 
# Removing the trailing new line 
chomp($email); 
 
# Pattern for Email validation 
my $pattern= '^([a-zA-Z][\w\_\.]{6,15})\@([a-zA-Z0-9.-]+)\.([a-zA-Z]{2,4})$'; 
 
# To find 

Open in new window

0
 
LVL 6

Author Comment

by:Sanjeev Jaiswal
Comment Utility
Yes read the second point:
Username can be a mixture of character, digit and _ (usually) of any length but it should start with only character i.e from a-z or A-Z (I restrcited length from 8-15, you can put your own).


username could be anything but its good if it would start with a-z or A-Z for your comfort. Thats why I wrote like this.
There can many possible solutions for it.

but how may of users use exceptional email ids?
I was just trying to give an idea that how you can write regex in Perl for email validation and everyone is free to modify it according to their use.

I use it in one of our portal where restriction is above mentioned.
ex: gmail never allows special charatcer. so what do we say then?
RFC is different and customizing it according to our need i different bro.

Thanks for the comment by the way :D
0
 
LVL 28

Expert Comment

by:FishMonger
Comment Utility
IMO, it would be better to use one of the email validation modules on cpan which include much better and more extensive validation.

Here's the first one that I thought of:
Email::Valid - Check validity of Internet email addresses
0
Many time we need to work with multiple files all together. If its windows system then we can use some GUI based editor to accomplish our task. But what if you are on putty or have only CLI(Command Line Interface) as an option to  edit your files. I prefer using vim editor.

After doing some experiments and knowing some important commands, I though to share with you. So i have written few commands point wise which may be useful for you while editing multiple files using vim (not GVIM, although these commands are valid there too.)

Tested on Linux box under vim v6.3 and vim v7.1


    Open multiple files in vim altogether
        Vim file1 file2 file3 ….
        Vim dirPath/pattern for files like vim perlfiles/*.pl     open multiple files in vim
        It will load in buffer simultaneously so that you can go back and forth to navigate files
   Open files in vim one by one
        Vim file1
        After opening it you can type :e file2 .    This command will be used only to open one file in buffer at one time.     use of :e command in vim
        If you try to open more it will throw error. For ex:  :e file2 file3    error while using :e in vim for multiple files
        It will not load instantly in buffer.
        You need to load it manually by browsing that file for the first time. Use command :b filename(now it will be loaded in buffer) ex:   :b file2
        In vim v7 we can use :tabe filename too (to see filename in tab)
   …
1
On Microsoft Windows, if  when you click or type the name of a .pl file, you get an error "is not recognized as an internal or external command, operable program or batch file", then this means you do not have the .pl file extension associated with the perl interpreter.  This is normally done by the installer, but not always.

To associate files:
    1) Start Windows explorer
    2) Select, from the menu, Tools -> Folder Options
    3) Select the File Types tab
    4)  Look in the list for a PL extension.  If it does NOT exist:
        4a) Click New
        4b) Type pl for extension
    5) Select the PL extension in the list
    6) In the section "opens with", click Change
    7) Select perl.exe
    8) Click OK until all Windows are closed.
2
 
LVL 40

Expert Comment

by:mrjoltcola
Comment Utility
Changing status to notify. Apologies, the articles system is still sort of a "beta" status. Please see my 2 comments above.
0
 
LVL 39

Author Comment

by:Adam314
Comment Utility
I'll update the instructions to work with Vista also.  I only have XP to test, but from your notes, it seems that the right-click method works on both XP and vista, so I'll provide instructions for that method.
0
I've just discovered very important differences between Windows an Unix formats in Perl,at least 5.xx..
MOST IMPORTANT:
Use Unix file format while saving Your script.
otherwise it will have ^M s or smth likely weird in the EOL,
Then DO NOT use my ad hoc initialization in Unix like my $list = ...;
,never worked,doesn't seem to work now,why bother,
just
compare 2 snips bellow:
#!/usr/bin/perl
use Win32::ODBC;
use File::Find;
#@ARGV = ('.') unless @ARGV;
$path = "/stage001/TgtFiles/CMR/";
#my    $filesize = 0;
$path = "c:\\DATA\\FTP\\" ;
$ret = 1;
$i = 1;
$file = "LOSEOUT_DTL.txt";
$trg =  655;
 
 if (@ARGV ne ("")) {
   $file = shift @ARGV;
   }
 
 
#   if (&checks) {    system 'gen_geo_files.ksh $file NN'   }
   &checks;
 
       sub checks(){
 
 
 
 
 
            #    my path = "/ushhetl/stage001/TgtFiles/CMR/"
 
            open (DATAFILE, $path.$file)
	or die ("Problem opening file: $!");
 
        while (defined ($line = <DATAFILE>)) {
    chomp $line;
 
    $size = length $line;
    if ($i==1) {$trg=($size-1); }
   if ( $size<$trg) {
             print "$i:$size:$file:$ret" ;
           print "$line\n";
               $ret  ;
 
}
 $i  ;
#  if ($i>0) {              return   $ret;  }
               # output size of line
}
  #  $filesize = -s $file;
                 print  "\n$file:$i:$trg";
 #if ($i>0) {
               return   $ret;
#}
}
#while (<SPREADSHEET>) {
 
  #	$numbers = $_;
   #	chomp($numbers);
 
    #	@numbers = split(/\t/, $numbers);
 
 #	

Open in new window

0
 
LVL 28

Expert Comment

by:FishMonger
Comment Utility
That's some of the worst Perl code I've ever seen.
0

Perl

25K

Solutions

10K

Contributors

Perl is a high-level, general-purpose, interpreted, dynamic programming languages with over 25 years of development. Perl 5 runs on over 100 platforms from portables to mainframes and is suitable for both rapid prototyping and large-scale development projects.  Perl gained widespread popularity as a Common Gateway Interface (CGI) scripting language, in part due to its regular expression and string parsing abilities. In addition to CGI, Perl is used for graphics programming, system administration, network programming, finance, bioinformatics, and other applications.