| uestions : 1 | What is Full form of PHP ? Who is the father or inventor of PHP ? | ||||||||||||||||||||
| Answers : 1 | Rasmus Lerdorf is known as the father of PHP that started development of PHP in 1994 for their own Personal Home Page (PHP) and they released PHP/FI (Forms Interpreter) version 1.0 publicly on 8 June 1995 But in 1997 two Israeli developers named Zeev Suraski and Andi Gutmans rewrote the parser that formed the base of PHP 3 and then changed the language's name to the PHP: Hypertext Preprocessor. | ||||||||||||||||||||
| Questions : 2 | What are the differences between PHP3 and PHP4 and PHP5 ? what is the current stable version of PHP ? what advance thing in php6 | ||||||||||||||||||||
| Answers : 2 | The current stable version of PHP is PHP 5.4.11 on 2013-01-17 as still waiting for PHP6with unicode handlig thing There are lot of difference among PHP3 and PHP4 and PHP5 version of php so Difference mean oldest version have less functionality as compare to new one like | ||||||||||||||||||||
| Questions : 3 | Is variable name casesensitive ? could we start a variale with number like $4name ? What is the difference between $name and $$name? | ||||||||||||||||||||
| Answer : 3 | Yes variable name casesensitive and we can not start a variable with number like $4name as A valid variable name starts with a letter or underscore, followed by any number of letters, numbers, or underscores. where as $$ is variable of variable $name is variable where as $$name is variable of variable like $name=sonia and $$name=singh so $sonia value is singh. | ||||||||||||||||||||
| Questions : 4 | What is use of header() function in php ? What the Limitation of HEADER()? | ||||||||||||||||||||
| Answers : 4 | In
PHP Important to notice the Limitation of HEADER() function is that
header() must be called before any actual output is send. Means must use
header function before HTML or any echo stateament There are Number of Use of HEADER() function in php like below 1> The header() function use to sends a raw HTTP header to a client. 2> We can use herder() function for redirection of pages. 3> Use for refresh the page on given time interval automatically. 4> To send email header content like cc, bcc , reply to etc data and lot more . | ||||||||||||||||||||
| Questions : 5 | How can we extract string "pcds.co.in" from a string "http://info@pcds.co.in" using regular expression of PHP? More on Reg can you explain | ||||||||||||||||||||
| Answers : 5 | We can extract string "pcds.co.in" using this preg_match("/^http:\/\/.+@(.+)$/","http://info@pcds.co.in",$matches); echo $matches[1]; More On regular expression interview question with very nice examples in | ||||||||||||||||||||
| Questions : 6 | How do you connet mysql database with PHP ? | ||||||||||||||||||||
| Answer : 6 | We can connect Mysql Database with PHP using both Procedural and Object oriented style like below $link = mysqli_connect("localhost", "username", "password", "dbofpcds"); $mysqli = new mysqli("localhost", "username", "password", "dbname"); and in old type of connectivity were $link = mysql_connect("localhost", "username", "password"); mysql_select_db("database",$link); | ||||||||||||||||||||
| Questions : 7 | In how many ways we can retrieve the data in the result set of MySQL using PHP? What is the difference between mysql_fetch_object and mysql_fetch_array ? | ||||||||||||||||||||
| Answers : 7 | we can retrieve the data in the result set of MySQL using PHP in 4 Ways 1. mysqli_fetch_row >> Get a result row as an enumerated array 2. mysqli_fetch_array >> Fetch a result row as associative and numeric array 3.mysqli_fetch_object >> Returns the current row of a result set as an object 4. mysqli_fetch_assoc >> Fetch a result row as an associative array mysqli_fetch_object() is similar to mysqli_fetch_array(), with one difference - an object is returned, instead of an array. Indirectly, that means that we can only access the data by the field names, and not by their offsets (numbers are illegal property names). | ||||||||||||||||||||
| Questions : 8 | How can we create a database using PHP and MySQL? | ||||||||||||||||||||
| Answers : 8 | We can create MySQL database with the use of mysql_create_db("Database Name") | ||||||||||||||||||||
| Questions : 9 | What are the differences between require and include? | ||||||||||||||||||||
| Answers : 9 | Both include and require used to include a file but when included file not found Include send Warning where as Require send Fatal Error . | ||||||||||||||||||||
| Questions : 10 | Can we use include ("xyz.PHP") two times in a PHP page "index.PHP"? | ||||||||||||||||||||
| Answers : 10 | Yes we can use include("xyz.php") more than one time in any page. but it create a prob when xyz.php file contain some funtions declaration then error will come for already declared function in this file else not a prob like if you want to show same content two time in page then must incude it two time not a prob | ||||||||||||||||||||
| Questions : 11 | What are the different tables(Engine) present in MySQL, which one is default? | ||||||||||||||||||||
| Answers : 11 | Following tables (Storage Engine) we can create 1. MyISAM(The default storage engine IN MYSQL Each MyISAM table is stored on disk in three files. The files have names that begin with the table name and have an extension to indicate the file type. An .frm file stores the table format. The data file has an .MYD (MYData) extension. The index file has an .MYI (MYIndex) extension. ) 2. InnoDB(InnoDB is a transaction-safe (ACID compliant) storage engine for MySQL that has commit, rollback, and crash-recovery capabilities to protect user data.) 3. Merge 4. Heap (MEMORY)(The MEMORY storage engine creates tables with contents that are stored in memory. Formerly, these were known as HEAP tables. MEMORY is the preferred term, although HEAP remains supported for backward compatibility. ) 5. BDB (BerkeleyDB)(Sleepycat Software has provided MySQL with the Berkeley DB transactional storage engine. This storage engine typically is called BDB for short. BDB tables may have a greater chance of surviving crashes and are also capable of COMMIT and ROLLBACK operations on transactions) 6. EXAMPLE 7. FEDERATED (It is a storage engine that accesses data in tables of remote databases rather than in local tables. ) 8. ARCHIVE (The ARCHIVE storage engine is used for storing large amounts of data without indexes in a very small footprint. ) 9. CSV (The CSV storage engine stores data in text files using comma-separated values format.) 10. BLACKHOLE (The BLACKHOLE storage engine acts as a "black hole" that accepts data but throws it away and does not store it. Retrievals always return an empty result) | ||||||||||||||||||||
| Questions : 12 | What are the differences between Get and post methods. | ||||||||||||||||||||
| Answers : 12 |
There are some defference between GET and POST method
1. GET Method have some limit like only 2Kb data able to send for request But in POST method unlimited data can we send 2. when we use GET method requested data show in url but Not in POST method so POST method is good for send sensetive request | ||||||||||||||||||||
| Questions : 13 | How can I execute a PHP script using command line? | ||||||||||||||||||||
| Answers : 13 | Just run the PHP CLI (Command Line Interface) program and provide the PHP script file name as the command line argument. | ||||||||||||||||||||
| Questions : 14 | Suppose your Zend engine supports the mode <? ?> Then how can u configure your PHP Zend engine to support <?PHP ?> mode ? | ||||||||||||||||||||
| Answers : 14 | In php.ini file: set short_open_tag=on to make PHP support | ||||||||||||||||||||
| Questions : 15 | Shopping cart online validation i.e. how can we configure Paypal, etc.? | ||||||||||||||||||||
| Answers : 15 | Nothing more we have to do only redirect to the payPal url after submit all information needed by paypal like amount,adresss etc. | ||||||||||||||||||||
| Questions : 16 | What is meant by nl2br()? | ||||||||||||||||||||
| Answers : 16 | Inserts HTML line breaks (<BR />) before all newlines in a string. | ||||||||||||||||||||
| Questions : 17 | What is htaccess? Why do we use this and Where? | ||||||||||||||||||||
| Answers : 17 | .htaccess files are configuration files of Apache Server which provide a way to make configuration changes on a per-directory basis. A file, containing one or more configuration directives, is placed in a particular document directory, and the directives apply to that directory, and all subdirectories thereof. | ||||||||||||||||||||
| Questions : 18 | How we get IP address of client, previous reference page etc ? | ||||||||||||||||||||
| Answers : 18 | By using $_SERVER['REMOTE_ADDR'],$_SERVER['HTTP_REFERER'] etc. | ||||||||||||||||||||
| Questions : 19 | What are the reasons for selecting lamp (Linux, apache, MySQL, PHP) instead of combination of other software programs, servers and operating systems? | ||||||||||||||||||||
| Answers : 19 | All of those are open source resource. Security of Linux is very very more than windows. Apache is a better server that IIS both in functionality and security. MySQL is world most popular open source database. PHP is more faster that asp or any other scripting language. | ||||||||||||||||||||
| Questions : 20 | How can we encrypt and decrypt a data present in a MySQL table using MySQL? | ||||||||||||||||||||
| Answers : 20 | AES_ENCRYPT () and AES_DECRYPT () | ||||||||||||||||||||
| Questions : 21 | How can we encrypt the username and password using PHP? | ||||||||||||||||||||
| Answers : 21 | The functions in this section perform encryption and decryption, and compression and uncompression:
| ||||||||||||||||||||
| Questions : 22 | What are the features and advantages of object-oriented programming? | ||||||||||||||||||||
| Answers : 22 | One of the main advantages of OO programming is its ease of modification; objects can easily be modified and added to a system there by reducing maintenance costs. OO programming is also considered to be better at modeling the real world than is procedural programming. It allows for more complicated and flexible interactions. OO systems are also easier for non-technical personnel to understand and easier for them to participate in the maintenance and enhancement of a system because it appeals to natural human cognition patterns. For some systems, an OO approach can speed development time since many objects are standard across systems and can be reused. Components that manage dates, shipping, shopping carts, etc. can be purchased and easily modified for a specific system | ||||||||||||||||||||
| Questions : 23 | What are the differences between procedure-oriented languages and object-oriented languages? | ||||||||||||||||||||
| Answers : 23 | There are lot of difference between procedure language and object oriented like below 1>Procedure language easy for new developer but complex to understand whole software as compare to object oriented model 2>In Procedure language it is difficult to use design pattern mvc , Singleton pattern etc but in OOP you we able to develop design pattern 3>IN OOP language we able to ree use code like Inheritance ,polymorphism etc but this type of thing not available in procedure language on that our Fonda use COPY and PASTE . | ||||||||||||||||||||
| Questions : 24 | What is the use of friend function? | ||||||||||||||||||||
| Answers : 24 | Sometimes a function is best shared among a number of different classes. Such functions can be declared either as member functions of one class or as global functions. In either case they can be set to be friends of other classes, by using a friend specifier in the class that is admitting them. Such functions can use all attributes of the class which names them as a friend, as if they were themselves members of that class. A friend declaration is essentially a prototype for a member function, but instead of requiring an implementation with the name of that class attached by the double colon syntax, a global function or member function of another class provides the match. | ||||||||||||||||||||
| Questions : 25 | What are the differences between public, private, protected, static, transient, final and volatile? | ||||||||||||||||||||
| Answer : 25 | Public: Public declared items can be accessed everywhere. Protected: Protected limits access to inherited and parent classes (and to the class that defines the item). Private: Private limits visibility only to the class that defines the item. Static: A static variable exists only in a local function scope, but it does not lose its value when program execution leaves this scope. Final: Final keyword prevents child classes from overriding a method by prefixing the definition with final. If the class itself is being defined final then it cannot be extended. transient: A transient variable is a variable that may not be serialized. volatile: a variable that might be concurrently modified by multiple threads should be declared volatile. Variables declared to be volatile will not be optimized by the compiler because their value can change at any time. | ||||||||||||||||||||
| Questions : 26 | What are the different types of errors in PHP? | ||||||||||||||||||||
| Answer : 26 | Three are three types of errors:1. Notices: These are trivial, non-critical errors that PHP encounters while executing a script – for example, accessing a variable that has not yet been defined. By default, such errors are not displayed to the user at all – although, as you will see, you can change this default behavior.2. Warnings: These are more serious errors – for example, attempting to include() a file which does not exist. By default, these errors are displayed to the user, but they do not result in script termination.3. Fatal errors: These are critical errors – for example, instantiating an object of a non-existent class, or calling a non-existent function. These errors cause the immediate termination of the script, and PHP's default behavior is to display them to the user when they take place. | ||||||||||||||||||||
| Questions : 27 | What is the functionality of the function strstr and stristr? | ||||||||||||||||||||
| Answers : 27 | strstr Returns part of string from the first occurrence of needle(sub string that we finding out ) to the end of string. $email= 'sonialouder@gmail.com'; $domain = strstr($email, '@'); echo $domain; // prints @gmail.com here @ is the needle stristr is case-insensitive means able not able to diffrenciate between a and A | ||||||||||||||||||||
| Questions : 28 | How can we submit a form without a submit button? | ||||||||||||||||||||
| Answer : 28 | Java script submit() function is used for submit form without submit button on click call document.formname.submit() | ||||||||||||||||||||
| Questions : 29 | How can we convert asp pages to PHP pages? | ||||||||||||||||||||
| Answer : 29 | there are lots of tools available for asp to PHP conversion. you can search Google for that. the best one is available athttp://asp2php.naken.cc./ | ||||||||||||||||||||
| Questions : 30 | What is the functionality of the function htmlentities? | ||||||||||||||||||||
| Answer : 30 | Convert all applicable characters to HTML entities This function is identical to htmlspecialchars() in all ways, except with htmlentities(), all characters which have HTML character entity equivalents are translated into these entities. | ||||||||||||||||||||
| Questions : 31 | How can we get second of the current time using date function? | ||||||||||||||||||||
| Answer : 31 | $second = date("s"); | ||||||||||||||||||||
| Questions : 32 | How can we convert the time zones using PHP? | ||||||||||||||||||||
| Answer : 32 | For convert the time zones using PHP we have to first set time zone By using PHP function date_default_timezone_set() If we want to set time zone of 'Europe/London' we have to call this funtion as date_default_timezone_set('Europe/London') so Now generate the timestamp for that particular timezone, on Sept 1st, 2012 at 8 am $pcds = mktime(8, 0, 0, 9, 1, 2012); Now set the other timezone like US/Eastern date_default_timezone_set('US/Eastern'); date(DATE_RFC1123, $pcds) date(DATE_RFC1123, $pcds) Output the date in a standard format (RFC1123) | ||||||||||||||||||||
| Questions : 33 | What is meant by urlencode and urldocode? | ||||||||||||||||||||
| Answer : 33 | URLencode returns a string in which all non-alphanumeric characters except -_. have been replaced with a percent (%) sign followed by two hex digits and spaces encoded as plus (+) signs. It is encoded the same way that the posted data from a WWW form is encoded, that is the same way as in application/x-www-form-urlencoded media type.
urldecode decodes any %##
encoding in the given string. | ||||||||||||||||||||
| Questions : 34 | What is the difference between the functions unlink and unset? | ||||||||||||||||||||
| Answer : 34 | unlink() deletes the given file from the file system. unset() makes a variable undefined. | ||||||||||||||||||||
| Questions : 35 | How can we register the variables into a session? | ||||||||||||||||||||
| Answer : 35 | $_SESSION['name'] = "sonia"; | ||||||||||||||||||||
| Questions : 36 | How can we get the properties (size, type, width, height) of an image using PHP image functions? | ||||||||||||||||||||
| Answer : 36 | To know the Image type use exif_imagetype () function To know the Image size use getimagesize () function To know the image width use imagesx () function To know the image height use imagesy() function t | ||||||||||||||||||||
| Questions : 37 | How can we get the browser properties using PHP? | ||||||||||||||||||||
| Answer : 37 | By using | ||||||||||||||||||||
| Questions : 38 | What is the maximum size of a file that can be uploaded using PHP and how can we change this? | ||||||||||||||||||||
| Answer : 38 | By default the maximum size is 2MB. and we can change the following setup at php.iniupload_max_filesize = 2M | ||||||||||||||||||||
| Questions : 39 | How can we increase the execution time of a PHP script? | ||||||||||||||||||||
| Answer : 39 | by changing the following setup at php.inimax_execution_time = 30 ; Maximum execution time of each script, in seconds | ||||||||||||||||||||
| Questions : 40 | How can we take a backup of a MySQL table and how can we restore it. ? | ||||||||||||||||||||
| Answer : 40 | To backup: BACKUP TABLE tbl_name[,tbl_name…] TO '/path/to/backup/directory' RESTORE TABLE tbl_name[,tbl_name…] FROM '/path/to/backup/directory'mysqldump: Dumping Table Structure and DataUtility to dump a database or a collection of database for backup or for transferring the data to another SQL server (not necessarily a MySQL server). The dump will contain SQL statements to create the table and/or populate the table. -t, –no-create-info Don't write table creation information (the CREATE TABLE statement). -d, –no-data Don't write any row information for the table. This is very useful if you just want to get a dump of the structure for a table! | ||||||||||||||||||||
| Questions : 41 | How can we optimize or increase the speed of a MySQL select query? | ||||||||||||||||||||
| Answer : 41 |
| ||||||||||||||||||||
| Questions : 42 | How many ways can we get the value of current session id? | ||||||||||||||||||||
| Answer : 42 | session_id() returns the session id for the current session. | ||||||||||||||||||||
| Questions : 43 | How can we destroy the session, how can we unset the variable of a session? | ||||||||||||||||||||
| Answer : 43 | session_unregister — Unregister a global variable from the current session session_unset — Free all session variables | ||||||||||||||||||||
| Questions : 44 | How can we set and destroy the cookie n php? | ||||||||||||||||||||
| Answer : 44 | By using setcookie(name, value, expire, path, domain); function we can set the cookie in php ; Set the cookies in past for destroy. like setcookie("user", "sonia", time()+3600); for set the cookie setcookie("user", "", time()-3600); for destroy or delete the cookies; | ||||||||||||||||||||
| Questions : 45 | How many ways we can pass the variable through the navigation between the pages? | ||||||||||||||||||||
| Answer : 45 |
| ||||||||||||||||||||
| Questions : 46 | What is the difference between ereg_replace() and eregi_replace()? | ||||||||||||||||||||
| Answer : 46 | eregi_replace() function is identical to ereg_replace() except that this ignores case distinction when matching alphabetic characters.eregi_replace() function is identical to ereg_replace() except that this ignores case distinction when matching alphabetic characters. | ||||||||||||||||||||
| Questions : 47 | What are the different functions in sorting an array? | ||||||||||||||||||||
| Answer : 47 | Sort(), arsort(), asort(), ksort(), natsort(), natcasesort(), rsort(), usort(), array_multisort(), and uksort(). | ||||||||||||||||||||
| Questions : 48 | How can we know the count/number of elements of an array? | ||||||||||||||||||||
| Answer : 48 | 2 ways a) sizeof($urarray) This function is an alias of count() b) count($urarray) | ||||||||||||||||||||
| Questions : 49 | what is session_set_save_handler in PHP? | ||||||||||||||||||||
| Answer : 49 | session_set_save_handler() sets the user-level session storage functions which are used for storing and retrieving data associated with a session. This is most useful when a storage method other than those supplied by PHP sessions is preferred. i.e. Storing the session data in a local database. | ||||||||||||||||||||
| Questions : 50 | How can I know that a variable is a number or not using a JavaScript? | ||||||||||||||||||||
| Answer : 50 | bool is_numeric ( mixed var) Returns TRUE if var is a number or a numeric string, FALSE otherwise.or use isNaN(mixed var)The isNaN() function is used to check if a value is not a number. | ||||||||||||||||||||
| Questions : 51 | List out some tools through which we can draw E-R diagrams for mysql. | ||||||||||||||||||||
| Answer : 51 | Case Studio Smart Draw | ||||||||||||||||||||
| Questions : 52 | How can I retrieve values from one database server and store them in other database server using PHP? | ||||||||||||||||||||
| Answer : 52 | we can always fetch from one database and rewrite to another. here is a nice solution of it.$db1 = mysql_connect("host","user","pwd") mysql_select_db("db1", $db1); $res1 = mysql_query("query",$db1);$db2 = mysql_connect("host","user","pwd") mysql_select_db("db2", $db2); $res2 = mysql_query("query",$db2);At this point you can only fetch records from you previous ResultSet, i.e $res1 – But you cannot execute new query in $db1, even if you supply the link as because the link was overwritten by the new db.so at this point the following script will fail $res3 = mysql_query("query",$db1); //this will failSo how to solve that?
take a look below.
$db1 = mysql_connect("host","user","pwd") mysql_select_db("db1", $db1); $res1 = mysql_query("query",$db1);
$db2 = mysql_connect("host","user","pwd", true)
mysql_select_db("db2", $db2); $res2 = mysql_query("query",$db2);
So mysql_connect has another optional boolean parameter which
indicates whether a link will be created or not. as we connect to the $db2 with this optional parameter set to 'true', so both link will remain live.
now the following query will execute successfully.
$res3 = mysql_query("query",$db1); | ||||||||||||||||||||
| Questions : 53 | List out the predefined classes in PHP? | ||||||||||||||||||||
| Answer : 53 | Directory stdClass __PHP_Incomplete_Class exception php_user_filter | ||||||||||||||||||||
| Questions : 54 | How can I make a script that can be bi-language (supports English, German)? | ||||||||||||||||||||
| Answer : 54 | You can maintain two separate language file for each of the language. all the labels are putted in both language files as variables and assign those variables in the PHP source. on runtime choose the required language option. | ||||||||||||||||||||
| Questions : 55 | What are the difference between abstract class and interface? | ||||||||||||||||||||
| Answer : 55 | Abstract class: abstract classes are the class where one or more methods are abstract but not necessarily all method has to be abstract. Abstract methods are the methods, which are declare in its class but not define. The definition of those methods must be in its extending class.Interface: Interfaces are one type of class where all the methods are abstract. That means all the methods only declared but not defined. All the methods must be define by its implemented class. | ||||||||||||||||||||
| Questions : 56 | How can we send mail using JavaScript? | ||||||||||||||||||||
| Answer : 56 | JavaScript does not have any networking capabilities as it is designed to work on client site. As a result we can not send mails using JavaScript. But we can call the client side mail protocol mailto via JavaScript to prompt for an email to send. this requires the client to approve it. | ||||||||||||||||||||
| Questions : 57 | How can we repair a MySQL table? | ||||||||||||||||||||
| Answer : 57 | The syntex for repairing a MySQL table is REPAIR TABLENAME, [TABLENAME, ], [Quick],[Extended] This command will repair the table specified if the quick is given the MySQL will do a repair of only the index tree if the extended is given it will create index row by row | ||||||||||||||||||||
| Questions : 58 | What are the advantages of stored procedures, triggers, indexes? | ||||||||||||||||||||
| Answer : 58 | A stored procedure is a set of SQL commands that can be compiled and stored in the server. Once this has been done, clients don't need to keep re-issuing the entire query but can refer to the stored procedure. This provides better overall performance because the query has to be parsed only once, and less information needs to be sent between the server and the client. You can also raise the conceptual level by having libraries of functions in the server. However, stored procedures of course do increase the load on the database server system, as more of the work is done on the server side and less on the client (application) side.Triggers will also be implemented. A trigger is effectively a type of stored procedure, one that is invoked when a particular event occurs. For example, you can install a stored procedure that is triggered each time a record is deleted from a transaction table and that stored procedure automatically deletes the corresponding customer from a customer table when all his transactions are deleted.Indexes are used to find rows with specific column values quickly. Without an index, MySQL must begin with the first row and then read through the entire table to find the relevant rows. The larger the table, the more this costs. If the table has an index for the columns in question, MySQL can quickly determine the position to seek to in the middle of the data file without having to look at all the data. If a table has 1,000 rows, this is at least 100 times faster than reading sequentially. If you need to access most of the rows, it is faster to read sequentially, because this minimizes disk seeks. | ||||||||||||||||||||
| Questions : 59 | What is the maximum length of a table name, database name, and fieldname in MySQL? | ||||||||||||||||||||
| Answer : 59 | The following table describes the maximum length for each type of identifier.
There are some restrictions on the characters that may appear in
identifiers: | ||||||||||||||||||||
| Questions : 60 | How many values can the SET function of MySQL take? | ||||||||||||||||||||
| Answer : 60 | MySQL set can take zero or more values but at the maximum it can take 64 values | ||||||||||||||||||||
| Questions : 61 | What are the other commands to know the structure of table using MySQL commands except explain command? | ||||||||||||||||||||
| Answer : 61 | describe Table-Name; | ||||||||||||||||||||
| Questions : 62 | How many tables will create when we create table, what are they? | ||||||||||||||||||||
| Answer : 62 | The '.frm' file stores the table definition. The data file has a '.MYD' (MYData) extension. The index file has a '.MYI' (MYIndex) extension, | ||||||||||||||||||||
| Questions : 63 | What is the purpose of the following files having extensions 1) .frm 2) .myd 3) .myi? What do these files contain? | ||||||||||||||||||||
| Answer : 63 | In MySql, the default table type is MyISAM. Each MyISAM table is stored on disk in three files. The files have names that begin with the table name and have an extension to indicate the file type. The '.frm' file stores the table definition. The data file has a '.MYD' (MYData) extension. The index file has a '.MYI' (MYIndex) extension, | ||||||||||||||||||||
| Questions : 64 | What is maximum size of a database in MySQL? | ||||||||||||||||||||
| Answer : 64 | If the operating system or filesystem places a limit on the number of files in a directory, MySQL is bound by that constraint.The efficiency of the operating system in handling large numbers of files in a directory can place a practical limit on the number of tables in a database. If the time required to open a file in the directory increases significantly as the number of files increases, database performance can be adversely affected. The amount of available disk space limits the number of tables. MySQL 3.22 had a 4GB (4 gigabyte) limit on table size. With the MyISAM storage engine in MySQL 3.23, the maximum table size was increased to 65536 terabytes (2567 – 1 bytes). With this larger allowed table size, the maximum effective table size for MySQL databases is usually determined by operating system constraints on file sizes, not by MySQL internal limits.The InnoDB storage engine maintains InnoDB tables within a tablespace that can be created from several files. This allows a table to exceed the maximum individual file size. The tablespace can include raw disk partitions, which allows extremely large tables. The maximum tablespace size is 64TB. The following table lists some examples of operating system file-size limits. This is only a rough guide and is not intended to be definitive. For the most up-to-date information, be sure to check the documentation specific to your operating system. Operating System File-size LimitLinux 2.2-Intel 32-bit 2GB (LFS: 4GB) Linux 2.4+ (using ext3 filesystem) 4TB Solaris 9/10 16TB NetWare w/NSS filesystem 8TB Win32 w/ FAT/FAT32 2GB/4GB Win32 w/ NTFS 2TB (possibly larger) MacOS X w/ HFS+ 2TB | ||||||||||||||||||||
| Questions : 65 | Give the syntax of Grant and Revoke commands? | ||||||||||||||||||||
| Answer : 65 | The generic syntax for grant is as following > GRANT [rights] on [database/s] TO [username@hostname] IDENTIFIED BY [password] now rights can be a) All privileges b) combination of create, drop, select, insert, update and delete etc.We can grant rights on all databse by using *.* or some specific database by database.* or a specific table by database.table_name username@hotsname can be either username@localhost, username@hostname and username@% where hostname is any valid hostname and % represents any name, the *.* any condition password is simply the password of userThe generic syntax for revoke is as following > REVOKE [rights] on [database/s] FROM [username@hostname] now rights can be as explained above a) All privileges b) combination of create, drop, select, insert, update and delete etc. username@hotsname can be either username@localhost, username@hostname and username@% where hostname is any valid hostname and % represents any name, the *.* any condition | ||||||||||||||||||||
| Questions : 66 | Explain Normalization concept? | ||||||||||||||||||||
| Answer : 66 | The normalization process involves getting our data to conform to three progressive normal forms, and a higher level of normalization cannot be achieved until the previous levels have been achieved (there are actually five normal forms, but the last two are mainly academic and will not be discussed).First Normal FormThe First Normal Form (or 1NF) involves removal of redundant data from horizontal rows. We want to ensure that there is no duplication of data in a given row, and that every column stores the least amount of information possible (making the field atomic).Second Normal FormWhere the First Normal Form deals with redundancy of data across a horizontal row, Second Normal Form (or 2NF) deals with redundancy of data in vertical columns. As stated earlier, the normal forms are progressive, so to achieve Second Normal Form, your tables must already be in First Normal Form.Third Normal Form
I have a confession to make; I do not often use Third Normal Form. In
Third Normal Form we are looking for data in our tables that is not fully dependant on the primary key, but dependant on another value in the table | ||||||||||||||||||||
| Questions : 67 | How can we find the number of rows in a table using MySQL? | ||||||||||||||||||||
| Answer : 67 | Use this for mysql >SELECT COUNT(*) FROM table_name; | ||||||||||||||||||||
| Questions : 68 | How can we find the number of rows in a result set using PHP? | ||||||||||||||||||||
| Answer : 68 | $result = mysql_query($sql, $db_link); | ||||||||||||||||||||
| Questions : 69 | How many ways we can we find the current date using MySQL? | ||||||||||||||||||||
| Answer : 69 | SELECT CURDATE(); CURRENT_DATE() = CURDATE() for time use SELECT CURTIME(); CURRENT_TIME() = CURTIME() | ||||||||||||||||||||
| Questions : 70 | What are the advantages and disadvantages of Cascading Style Sheets? | ||||||||||||||||||||
| Answer : 70 | External Style SheetsAdvantagesCan control styles for multiple documents at once. Classes can be created for use on multiple HTML element types in many documents. Selector and grouping methods can be used to apply styles under complex contextsDisadvantagesAn extra download is required to import style information for each document The rendering of the document may be delayed until the external style sheet is loaded Becomes slightly unwieldy for small quantities of style definitionsEmbedded Style Sheets
Advantages
Classes can be created for use on multiple tag types in the document.
Selector and grouping methods can be used to apply styles under complex contexts. No additional downloads necessary to receive style information
Disadvantages
This method can not control styles for multiple documents at once
Inline Styles
Advantages
Useful for small quantities of style definitions. Can override other
style specification methods at the local level so only exceptions need to be listed in conjunction with other style methods
Disadvantages
Does not distance style information from content (a main goal of
SGML/HTML). Can not control styles for multiple documents at once. Author can not create or control classes of elements to control multiple element types within the document. Selector grouping methods can not be used to create complex element addressing scenarios | ||||||||||||||||||||
| Questions : 71 | What type of inheritance that PHP supports? | ||||||||||||||||||||
| Answer : 71 | In PHP an extended class is always dependent on a single base class, that is, multiple inheritance is not supported. Classes are extended using the keyword 'extends'. | ||||||||||||||||||||
| Questions : 72 | What is the difference between Primary Key and Unique key? | ||||||||||||||||||||
| Answer : 72 | Primary Key: A column in a table whose values uniquely identify the rows in the table. A primary key value cannot be NULL.
Unique Key: Unique Keys are used to uniquely identify each row in the
table. There can be one and only one row for each unique key value. So NULL can be a unique key.There can be only one primary key for a table but there can be more than one unique for a table. | ||||||||||||||||||||
Question : 73 | what is garbage collection? default time ? refresh time? | ||||||||||||||||||||
| Answer : 73 | Garbage Collection is an automated part of PHP , If the Garbage Collection process runs, it then analyzes any files in the /tmp for any session files that have not been accessed in a certain amount of time and physically deletes them. Garbage Collection process only runs in the default session save directory, which is /tmp. If you opt to save your sessions in a different directory, the Garbage Collection process will ignore it. the Garbage Collection process does not differentiate between which sessions belong to whom when run. This is especially important note on shared web servers. If the process is run, it deletes ALL files that have not been accessed in the directory. There are 3 PHP.ini variables, which deal with the garbage collector: PHP ini value name default session.gc_maxlifetime 1440 seconds or 24 minutes session.gc_probability 1 session.gc_divisor 100 | ||||||||||||||||||||
| Questions : 74 | What are the advantages/disadvantages of MySQL and PHP? | ||||||||||||||||||||
| Answer : 74 | Both of them are open source software (so free of cost), support cross platform. php is faster then ASP and JSP. | ||||||||||||||||||||
| Questions : 75 | What is the difference between GROUP BY and ORDER BY in Sql? | ||||||||||||||||||||
| Answer : 75 | ORDER BY [col1],[col2],…,[coln]; Tels DBMS according to what columns it should sort the result. If two rows will hawe the same value in col1 it will try to sort them according to col2 and so on.GROUP BY [col1],[col2],…,[coln]; Tels DBMS to group results with same value of column col1. You can use COUNT(col1), SUM(col1), AVG(col1) with it, if you want to count all items in group, sum all values or view average | ||||||||||||||||||||
| Questions : 76 | What is the difference between char and varchar data types? | ||||||||||||||||||||
| Answer : 76 | Set char to occupy n bytes and it will take n bytes even if u r storing a value of n-m bytes Set varchar to occupy n bytes and it will take only the required space and will not use the n bytes eg. name char(15) will waste 10 bytes if we store 'romharshan', if each char takes a byte eg. name varchar(15) will just use 5 bytes if we store 'romharshan', if each char takes a byte. rest 10 bytes will be free. | ||||||||||||||||||||
| Questions : 77 | What is the functionality of md5 function in PHP? | ||||||||||||||||||||
| Answer : 77 | Calculate the md5 hash of a string. The hash is a 32-character hexadecimal number. I use it to generate keys which I use to identify users etc. If I add random no techniques to it the md5 generated now will be totally different for the same string I am using. | ||||||||||||||||||||
| Questions : 78 | How can I load data from a text file into a table? | ||||||||||||||||||||
| Answer : 78 | you can use LOAD DATA INFILE file_name; syntax to load data from a text file. but you have to make sure thata) data is delimited b) columns and data matched correctly | ||||||||||||||||||||
| Questions : 79 | How can we know the number of days between two given dates using MySQL? | ||||||||||||||||||||
| Answer : 79 | SELECT DATEDIFF("2007-03-07","2005-01-01"); | ||||||||||||||||||||
| Questions : 80 | How can we know the number of days between two given dates using PHP? | ||||||||||||||||||||
| Answer : 80 | $date1 = date("Y-m-d"); $date2 = "2006-08-15"; $days = (strtotime($date1) - strtotime($date2)) / (60 * 60 * 24); | ||||||||||||||||||||
| Questions : 81 | How we load all classes that placed in different directory in one PHP File , means how to do auto load classes | ||||||||||||||||||||
| Answer : 81 | by using spl_autoload_register('autoloader::funtion'); Like below class autoloader { public static function moduleautoloader($class) { $path = $_SERVER['DOCUMENT_ROOT'] . "/modules/{$class}.php"; if (is_readable($path)) require $path; } public static function daoautoloader($class) { $path = $_SERVER['DOCUMENT_ROOT'] . "/dataobjects/{$class}.php"; if (is_readable($path)) require $path; } public static function includesautoloader($class) { $path = $_SERVER['DOCUMENT_ROOT'] . "/includes/{$class}.php"; if (is_readable($path)) require $path; } } spl_autoload_register('autoloader::includesautoloader'); spl_autoload_register('autoloader::daoautoloader'); spl_autoload_register('autoloader::moduleautoloader'); | ||||||||||||||||||||
| Questions : 82 | How many types of Inheritances used in PHP and how we achieve it | ||||||||||||||||||||
| Answer : 82 | As far PHP concern it only support single Inheritance in scripting. we can also use interface to achieve multiple inheritance. | ||||||||||||||||||||
| Questions : 83 | PHP how to know user has read the email? | ||||||||||||||||||||
| Answers : 83 | Using Disposition-Notification-To: in mailheader we can get read receipt. Add the possibility to define a read receipt when sending an email. It’s quite straightforward, just edit email.php, and add this at vars definitions: var $readReceipt = null; And then, at ‘createHeader’ function add: if (!empty($this->readReceipt)) { $this->__header .= ‘Disposition-Notification-To: ‘ . $this->__formatAddress($this->readReceipt) . $this->_newLine; } | ||||||||||||||||||||
| Questions : 84 | What are default session time and path? | ||||||||||||||||||||
| Answers : 84 | default session time in PHP is 1440 seconds or 24 minutes Default session save path id temporary folder /tmp | ||||||||||||||||||||
| Questions : 85 | how to track user logged out or not? when user is idle ? | ||||||||||||||||||||
| Answers : 85 | By checking the session variable exist or not while loading th page. As the session will exist longer as till browser closes. The default behaviour for sessions is to keep a session open indefinitely and only to expire a session when the browser is closed. This behaviour can be changed in the php.ini file by altering the line session.cookie_lifetime = 0 to a value in seconds. If you wanted the session to finish in 5 minutes you would set this to session.cookie_lifetime = 300 and restart your httpd server. | ||||||||||||||||||||
| Questions : 86 | how to track no of user logged in ? | ||||||||||||||||||||
| Answers : 86 | whenever a user logs in track the IP, userID etc..and store it in a DB with a active flag while log out or sesion expire make it inactive. At any time by counting the no: of active records we can get the no: of visitors. | ||||||||||||||||||||
| Questions : 87 | in PHP for pdf which library used? | ||||||||||||||||||||
| Answers : 87 | The PDF functions in PHP can create PDF files using the PDFlib library With version 6, PDFlib offers an object-oriented API for PHP 5 in addition to the function-oriented API for PHP 4. There is also the » Panda module. FPDF is a PHP class which allows to generate PDF files with pure PHP, that is to say without using the PDFlib library. F from FPDF stands for Free: you may use it for any kind of usage and modify it to suit your needs. FPDF requires no extension (except zlib to activate compression and GD for GIF support) and works with PHP4 and PHP5. | ||||||||||||||||||||
| Questions : 88 | for image work which library? | ||||||||||||||||||||
| Answers : 88 | we will need to compile PHP with the GD library of image functions for this to work. GD and PHP may also require other libraries, depending on which image formats you want to work with. | ||||||||||||||||||||
| Questions : 89 | what is design pattern? explain all including singleton pattern? | ||||||||||||||||||||
| Answers : 89 | A design pattern is a general reusable solution to a commonly occurring problem in software design. The Singleton design pattern allows many parts of a program to share a single resource without having to work out the details of the sharing themselves. | ||||||||||||||||||||
| Questions : 90 | what are magic methods? | ||||||||||||||||||||
| Answers : 90 | Magic methods are the members functions that is available to all the instance of class Magic methods always starts with "__". Eg. __construct All magic methods needs to be declared as public To use magic method they should be defined within the class or program scope Various Magic Methods used in PHP 5 are: __construct() __destruct() __set() __get() __call() __toString() __sleep() __wakeup() __isset() __unset() __autoload() __clone() | ||||||||||||||||||||
| Questions : 91 | what is magic quotes? | ||||||||||||||||||||
| Answers : 91 | Magic Quotes is a process that automagically escapes ncoming data to the PHP script. It’s preferred to code with magic quotes off and to instead escape the data at runtime, as needed. This feature has been DEPRECATED as of PHP 5.3.0 and REMOVED as of PHP 6.0.0. Relying on this feature is highly discouraged. | ||||||||||||||||||||
| Questions : 92 | what is cross site scripting? SQL injection? | ||||||||||||||||||||
| Answers : 92 | Cross-site scripting (XSS) is a type of computer security vulnerability typically found in web applications which allow code injection by malicious web users into the web pages viewed by other users. Examples of such code include HTML code and client-side scripts. SQL injection is a code injection technique that exploits a security vulnerability occurring in the database layer of an application. The vulnerability is present when user input is either incorrectly filtered for string literal escape characters embedded in SQL statements or user input is not strongly typed and thereby unexpectedly executed | ||||||||||||||||||||
| Questions : 93 | what is URL rewriting? | ||||||||||||||||||||
| Answers : 93 | Using URL rewriting we can convert dynamic URl to static URL Static URLs are known to be better than Dynamic URLs because of a number of reasons 1. Static URLs typically Rank better in Search Engines. 2. Search Engines are known to index the content of dynamic pages a lot slower compared to static pages. 3. Static URLs are always more friendlier looking to the End Users. along with this we can use URL rewriting in adding variables [cookies] to the URL to handle the sessions. | ||||||||||||||||||||
| Questions : 94 | what is the major php security hole? how to avoid? | ||||||||||||||||||||
| Answers : 94 | 1. Never include, require, or otherwise open a file with a filename based on user input, without thoroughly checking it first. 2. Be careful with eval() Placing user-inputted values into the eval() function can be extremely dangerous. You essentially give the malicious user the ability to execute any command he or she wishes! 3. Be careful when using register_globals = ON It was originally designed to make programming in PHP easier (and that it did), but misuse of it often led to security holes 4. Never run unescaped queries 5. For protected areas, use sessions or validate the login every time. 6. If you don’t want the file contents to be seen, give the file a .php extension. | ||||||||||||||||||||
| Questions : 95 | whether PHP supports Microsoft SQL server ? | ||||||||||||||||||||
| Answers : 95 | The SQL Server Driver for PHP v1.0 is designed to enable reliable, scalable integration with SQL Server for PHP applications deployed on the Windows platform. The Driver for PHP is a PHP 5 extension that allows the reading and writing of SQL Server data from within PHP scripts. using MSSQL or ODBC modules we can access Microsoft SQL server. | ||||||||||||||||||||
| Questions : 96 | what is MVC? why its been used? | ||||||||||||||||||||
| Answers : 96 | Model-view-controller
(MVC) is an architectural pattern used in software engineering.
Successful use of the pattern isolates business logic from user
interface considerations, resulting in an application where it is easier
to modify either the visual appearance of the application or the
underlying business rules without affecting the other. In MVC, the model
represents the information (the data) of the application; the view
corresponds to elements of the user interface such as text, checkbox
items, and so forth; and the controller manages the communication of
data and the business rules used to manipulate the data to and from the
model. WHY ITS NEEDED IS 1 Modular separation of function 2 Easier to
maintain 3 View-Controller separation means: A — Tweaking design (HTML) without altering code B — Web design staff can modify UI without understanding code | ||||||||||||||||||||
| Questions : 97 | what is framework? how it works? what is advantage? | ||||||||||||||||||||
| Answers : 97 | In general, a framework is a real or conceptual structure intended to serve as a support or guide for the building of something that expands the structure into something useful. Advantages : Consistent Programming Model Direct Support for Security Simplified Development Efforts Easy Application Deployment and Maintenance | ||||||||||||||||||||
| Questions : 98 | what is CURL? | ||||||||||||||||||||
| Answers : 98 | CURL means Client URL Library curl is a command line tool for transferring files with URL syntax, supporting FTP, FTPS, HTTP, HTTPS, SCP, SFTP, TFTP, TELNET, DICT, LDAP, LDAPS and FILE. curl supports SSL certificates, HTTP POST, HTTP PUT, FTP uploading, HTTP form based upload, proxies, cookies, user+password authentication (Basic, Digest, NTLM, Negotiate, kerberos…), file transfer resume, proxy tunneling and a busload of other useful tricks. CURL allows you to connect and communicate to many different types of servers with many different types of protocols. libcurl currently supports the http, https, ftp, gopher, telnet, dict, file, and ldap protocols. libcurl also supports HTTPS certificates, HTTP POST, HTTP PUT, FTP uploading (this can also be done with PHP’s ftp extension), HTTP form based upload, proxies, cookies, and user+password authentication. | ||||||||||||||||||||
| Questions : 99 | what is PDO ? | ||||||||||||||||||||
| Answers : 99 |
The PDO ( PHP Data Objects ) extension defines a lightweight, consistent
interface for accessing databases in PHP. if you are using the PDO API,
you could switch the database server you used, from say PgSQL to MySQL,
and only need to make minor changes to your PHP code.
While PDO has its advantages, such as a clean, simple, portable API but its maindisadvantage is
that it doesn't allow you to use all of the advanced features that are
available in the latest versions of MySQL server. For example, PDO does
not allow you to use MySQL's support for Multiple Statements.
Just need to use below code for connect mysql using PDOtry { $dbh = new PDO("mysql:host=$hostname;dbname=databasename", $username, $password); $sql = "SELECT * FROM employee"; foreach ($dbh->query($sql) as $row) { print $row['employee_name'] .' - '. $row['employee_age'] ; } } catch(PDOException $e) { echo $e->getMessage(); } | ||||||||||||||||||||
| Questions : 100 | What is PHP's mysqli Extension? | ||||||||||||||||||||
| Answers : 100 |
The mysqli extension, or as it is sometimes known, the MySQL improved
extension, was developed to take advantage of new features found in
MySQL systems versions 4.1.3 and newer. The mysqli extension is included
with PHP versions 5 and later.
The mysqli extension has a number of benefits, the key enhancements over the mysql extension being:=>Object-oriented interface =>Support for Prepared Statements =>Support for Multiple Statements =>Support for Transactions =>Enhanced debugging capabilities =>Embedded server support |
Tuesday, December 31, 2013
PHP Interview Questions and Answers
Monday, December 30, 2013
Interview Questions on Perl for freshers
Perl Basics
- 1. What is CPAN? Its uses?CPAN stands for Comprehensive Perl Archive Network. It is a vast, centralized repository of Perl software, modules, and documentation. Its primary use is to allow developers to easily search, download, and install third-party libraries so they do not have to reinvent the wheel for common tasks (e.g., database connection, web scraping).2. What does “use strict”, “use vars”, “no vars” mean?
use strict: A compile-time pragma that forces you to declare variables before use (usually withmy), disables soft references, and forbids barewords, drastically reducing typos and logical errors.use vars: An older way to pre-declare global package variables, allowing you to use them underuse strictwithout getting an error. Modern Perl typically usesourinstead.no vars: Disables the effects of a previoususe varsdeclaration within the current scope.
3. Difference b/w use and require?use: Evaluated at compile-time. It loads the module and automatically calls itsimportmethod to bring functions/variables into your current namespace.require: Evaluated at run-time. It loads the module or file (if not already loaded) but does not call theimportmethod, meaning you must use fully qualified names to access its contents.
4. What do the symbols $ @ and % mean when prefixing a variable?$(Scalar): Holds a single, discrete value (a number, string, or reference).@(Array): Holds an ordered list of scalar values, indexed by numbers starting at 0.%(Hash): Holds an unordered set of key-value pairs, where keys are unique strings.
5. What purpose does each of the following serve: -w, strict, -T?-w: A command-line flag that enables run-time warnings for questionable code, such as using uninitialized variables.strict: A pragma that enforces strict variable declaration, references, and subroutines at compile-time.-T: Enables "Taint mode," a security feature that marks all data coming from outside the program (like user input or environment variables) as untrusted. Tainted data cannot be used in system calls or file operations until explicitly sanitized.
6. Explain the use of do, require, use, importdo: Executes a file as a Perl script or evaluates a block of code and returns the value of the last expression evaluated.require: Loads a Perl file or module into memory at run-time, ensuring it is only loaded once per script execution.use: Loads a module at compile-time and automatically invokes itsimportmethod.import: A method within a module that is responsible for exporting specific symbols (functions, variables) into the namespace of the script that calleduse.
7. Use of our, my and local?my: Creates a lexically scoped variable. It is entirely private to the block, loop, or subroutine where it is declared.our: Creates an alias to a package-scoped global variable, allowing you to access a global variable legally underuse strictwithout typing its full package name.local: Temporarily saves the current value of an existing global variable, assigns it a new value for the duration of the current block, and restores the original value when the block exits.
8. Use of ref functionThereffunction checks if a scalar variable is a reference. If it is, it returns a string indicating the type of data being referenced (e.g.,SCALAR,ARRAY,HASH,CODE, or the class/package name for blessed objects). If it is not a reference, it returns an empty string (false).9. What are STDIN, STDOUT and STDERR?These are the standard, pre-opened filehandles provided by the operating system:STDIN: Standard Input. Used to read data into the script (default is the keyboard).STDOUT: Standard Output. Used to print normal data out of the script (default is the terminal screen).STDERR: Standard Error. Used to print error messages or diagnostics (also defaults to the terminal screen, but can be redirected separately from STDOUT).
10. @INC and %INC@INC: A special built-in array that contains the list of directory paths Perl searches when it is told to load a module viauseorrequire.%INC: A special built-in hash where the keys are the names of the modules/files that have already been loaded, and the values are the absolute file paths to those modules on the system. Perl uses this to avoid loading the same file twice.
- 11. How you can invoke shell command? Hint: system(), backtick, exec() in details
system(): Runs the shell command in a child process, waits for it to complete, and then returns control to the Perl script. It returns the exit status of the command, not its output. Output goes directly to standard output (STDOUT).- Backticks (
`...`) orqx(): Executes the command and captures its standard output. It returns the output as a string (in scalar context) or as a list of lines (in list context) back to the Perl script. exec(): Replaces the currently running Perl process entirely with the new shell command. Control never returns to the Perl script after a successfulexec().
12. What is the exact difference between system() and exec()The exact difference lies in process flow.system()forks a new child process to run the command, allowing the Perl script to resume execution once the command finishes.exec()terminates the current Perl script completely and replaces it in memory with the executed command. Any code written below anexec()statement will never be reached unless the command fails to execute.13. Error handling using evalIn Perl, fatal errors (like callingdieor dividing by zero) normally crash the script. By wrapping risky code in aneval { ... }block, you trap the exception. The script continues running, and any error message is stored in the special variable$@. If$@is true after the block, it indicates an error occurred.14. Print the array @arr in reversed case-insensitive orderYou use thesortfunction with a custom block usingcmpandlc(lowercase), reversing the positions of$aand$b:print sort { lc($b) cmp lc($a) } @arr;15. There are two types of eval statements i.e. eval EXPR and eval BLOCK. Explain them.eval BLOCK(eval { ... }): Code within the block is checked for syntax at compile-time, making it highly efficient. It is standardly used for exception handling (trapping fatal errors).eval EXPR(eval "..."): Code within the string is parsed, compiled, and executed at run-time. It is much slower and can be a massive security risk if the string contains untrusted user input, but it allows for dynamic code generation.
16. Autovivification (very rarely)Autovivification is Perl's feature of automatically creating references, arrays, or hashes when an undefined variable is dereferenced. For example, if%hashis completely empty, saying$hash{Person}{Name} = "Alice";will automatically create the nested hash reference for{Person}on the fly.17. Questions on hash and hash referenceA hash (%hash) is a primary data structure containing key-value pairs. A hash reference ($href = \%hash) is a single scalar variable containing the memory address of a hash. References are required when building complex, nested data structures (like arrays of hashes) or when passing entire hashes into subroutines without flattening them into a list. 18. Sort a hash by values Sort the keys of the hash based on the comparison of their fetched values:my @sorted_keys = sort { $hash{$a} <=>$hash{$b} } keys \%hash;(Use<=>for numeric values, orcmpfor string values). 19. Length of a hash To get the number of key-value pairs in a hash, evaluate thekeysfunction in a scalar context:my $hash_length = scalar keys %hash;20. How to refer a hash and how to dereference it- Referencing: Use a backslash on a named hash (
my $href = \%hash;) or create an anonymous hash reference (my $href = { key => 'value' };). - Dereferencing: Use the arrow operator for single values (
$href->{key}) or the\%sigil to extract the whole hash (\%{$href}).
21. Nesting of hashBecause Perl hashes can only store scalar values, you cannot place a literal hash inside another hash. To nest, you must use hash references as the values of the outer hash's keys:$company{IT} = { Manager => 'Bob', Headcount => 15 };22. Chomp and chop *chop: Blindly removes and returns the absolute last character of a string, no matter what it is. *chomp: Safely removes the trailing newline character (the current value of$/) from the end of a string. If there is no newline, it does nothing. It returns the number of characters removed.23. Explain qw, qq, qx (what’s its use, when to use)qw(...): Quote Words. Automatically extracts whitespace-separated words into a list. (e.g.,qw(red blue)is exactly identical to('red', 'blue')). Use it to type lists faster without commas or quotes.qq(...): Double-quote. Acts exactly like standard double quotes ("..."), interpolating variables within the string. Useful if your string contains literal quote marks so you don't have to escape them.qx(...): Quote Execute. Acts exactly like backticks (`...`) to execute a system command and return its output into the script. Useful for avoiding escaping issues with commands containing quotes.
- 24. What are the different ways to concatenate strings in Perl
- Dot Operator (
.): The standard string concatenation operator. Example:$result = $str1 . $str2; - Double-quote Interpolation: Embedding variables directly within double quotes. Example:
$result = "$str1$str2"; joinfunction: Best for combining an array of strings. Example:$result = join("", $str1, $str2);.=Operator: Appends a string to an existing variable. Example:$str1 .= $str2;
25. Functions on array: Push, pop, shift, unshift, join, split, splicepush: Appends one or more elements to the end of an array.pop: Removes and returns the last element of an array.shift: Removes and returns the first element of an array, shifting all other elements left.unshift: Prepends one or more elements to the beginning of an array, shifting existing elements right.join: Concatenates array elements into a single string, separated by a specified delimiter.split: Breaks a string into an array of substrings using a regular expression as the delimiter.splice: The most powerful array manipulator; it can add, remove, or replace elements anywhere within an array.
26. Different hash functions like each, keys, values, exists, deletekeys: Returns a list of all the keys in a hash.values: Returns a list of all the values in a hash.each: Iterates through a hash, returning a 2-element list containing the next key and value.exists: Returns true if a specified key exists in the hash, regardless of whether its value is defined orundef.delete: Removes a key and its associated value from the hash entirely.
27. Numeric and string sorting of hash and array elements in PerlThesortfunction relies on a block to determine comparison rules:- String sorting: Uses the
cmpoperator. Example:sort { $a cmp $b } @arr;(This is also the default if no block is provided). - Numeric sorting: Uses the
<=>operator. Example:sort { $a <=> $b } @arr; - Hash sorting: Hashes cannot be sorted directly. You must sort their
keys(often by comparing their values inside the sort block) and then iterate through the sorted keys.
28. How to set Environment variables in Perl (Hint: %ENV)Environment variables are accessed and modified using the special global hash%ENV.$ENV{'PATH'} = '/my/custom/path:' . $ENV{'PATH'};Changes made to%ENVonly affect the current Perl process and any child processes (like system commands) it spawns; they do not alter the parent shell's environment.29. Use of map and grep functions in Perlmap(Transformation): Applies an expression or block of code to every element in a list, returning a new list containing the results. Example:my @squares = map { $_ * $_ } @numbers;grep(Filtering): Evaluates an expression or block for every element in a list, returning a new list containing only the elements for which the expression returned true. Example:my @evens = grep { $_ % 2 == 0 } @numbers;
30. What is the difference between die and exit in perldie: Throws a fatal exception and prints an error message toSTDERR. It is used for error handling and its execution can be intercepted/caught using anevalblock.exit: Terminates the program normally and immediately returns a specific exit status code to the operating system. It is not an exception and cannot be caught byeval.
31. Array with repeating digits. Sort and print digits with no of counts. (No hash allowed, optimize)To achieve the exact output array(1,1,1,2,2,2,3,3,4,4,4,6,6,8,9), you simply use a numeric sort, which takes $O(N \log N)$ time:@arr = sort { $a <=> $b } @arr;If the goal is to also print the unique digits alongside their occurrence count without using a hash (which would take $O(N)$ time but is forbidden by the prompt), you can sort the array first, then iterate through it sequentially. This takes $O(N \log N)$ time and $O(1)$ auxiliary space:my @arr = (1,2,4,6,3,1,2,6,8,3,4,9,1,2,4); @arr = sort { $a <=> $b } @arr; my $count = 1; for (my $i = 0; $i < @arr; $i++) { if ($i < $#arr && $arr[$i] == $arr[$i+1]) { $count++; } else { print "Digit: $arr[$i], Count: $count\n"; $count = 1; # Reset for the next digit } }32. Write a Perl script for decimal to binary, decimal to octal, decimal to hex, hex to decimal, hex to octal, hex to binaryPerl handles conversions efficiently usingsprintffor formatting numbers into strings (binary, octal, hex) andhex()oroct()for converting strings back to decimal numbers.my $dec = 255; # Decimal to Others my $bin_str = sprintf("%b", $dec); # "11111111" my $oct_str = sprintf("%o", $dec); # "377" my $hex_str = sprintf("%x", $dec); # "ff" # Others to Decimal my $dec_from_hex = hex($hex_str); # 255 my $dec_from_oct = oct($oct_str); # 255 my $dec_from_bin = oct("0b" . $bin_str); # 255 # Hex to Binary/Octal (Convert Hex to Decimal first, then format) my $hex_to_bin = sprintf("%b", hex($hex_str));
my $hex_to_oct = sprintf("%o", hex($hex_str)); - 33. How to find which method is being invoked or being called in Perl and in which method you are currently? (Hint: caller())You can use the built-in
caller()function to inspect the call stack.- To find out information about the current subroutine's caller (who invoked you), use
caller(0). - To find the name of the method you are currently in, you look one level deeper in the stack using
caller(0)from a nested context, or typically(caller(0))[3]to get the subroutine name of the currently executing frame.Example:my ($package, $filename, $line, $subroutine) = caller(0);
34. Explain the use of caller and wantarray function?caller: Used to return information about the stack of current subroutine calls. It provides the package name, file name, line number, and subroutine name of the code that called the current function. It is highly useful for debugging and generating stack traces.wantarray: Detects the context in which a subroutine was called. It returns true (1) if the subroutine was called in a list context, false ("") if called in a scalar context, andundefif called in a void context (where the return value is discarded). This allows a subroutine to return different, context-appropriate data.
35. How will you find line number, package and a file name in Perl? (Hint: __FILE__, __LINE__, __PACKAGE__)You use Perl's special compile-time tokens:__LINE__: Returns the current line number of the script where the token is written.__FILE__: Returns the name of the current file being executed.__PACKAGE__: Returns the name of the current package being compiled (defaults tomain).
36. How to install a Perl Module from CPANFrom your system's command line, you can use the standard CPAN shell:cpan Module::NameAlternatively, many developers prefercpanm(CPAN Minus) for a quieter, more streamlined installation:cpanm Module::Name37. Pass an array to subroutine and get it as an array ref in sub. Hint: subroutine prototype. Ex: sub subname(@)By using the(\@)prototype, you force Perl to pass the array as a reference implicitly, without the caller needing to explicitly use the backslash\@arraysyntax.sub process_array (\@) { my $array_ref = shift; print "First element: $array_ref->[0]\n"; } my @data = (10, 20, 30); process_array(@data); # Passed implicitly as a reference38. You have two arrays. Say @arr1 = (1,2,5,6) and @arr2 = (3,4). Put the element of @arr2 at @arr1 so @arr1 becomes (1,2,3,4,5,6) without using a third array. Hint: use spliceThesplicefunction can insert elements into an array at a specific offset. You want to insert@arr2starting at index 2, removing 0 elements.my @arr1 = (1, 2, 5, 6); my @arr2 = (3, 4); splice(@arr1, 2, 0, @arr2); # @arr1 is now (1, 2, 3, 4, 5, 6)39. Inbuilt special variable like $!,$? Etc$!: The system error message (orerrno). It returns the numeric error code in a numeric context, or the string error message (like "File not found") in a string context after a failed system call or I/O operation. *$?: The status returned by the last pipe close, backtick (`) command, orsystem()operator. To get the actual exit value, you typically divide it by 256 ($? >> 8).$_: The default input and pattern-matching space (often called the "it" variable).$@: The Perl syntax error message from the lasteval()block.
40. Piping in and piping out in PerlPiping allows Perl to stream data to or from external system commands using theopenfunction.- Piping In (Read from a command): Open a filehandle with a trailing pipe.
open(my $in, '-|', 'ls -l') or die; - Piping Out (Write to a command): Open a filehandle with a leading pipe.
open(my $out, '|-', 'grep error') or die;
41. How to implement stack and queue in Perl?Perl arrays act natively as both stacks and queues using standard array functions.- Stack (LIFO - Last In, First Out): Use
pushto add elements to the top, andpopto remove them from the top. - Queue (FIFO - First In, First Out): Use
pushto add elements to the back, andshiftto remove them from the front.
42. How to find the list of installed modules in Perl?You can check the locally installed modules via the command line usingperldoc:perldoc perllocalAlternatively, you can use theinstmodshutility bundled with ExtUtils::MakeMaker, or run a one-liner to search through the@INCpath for.pmfiles.43. Use of BEGIN and END block.BEGIN { ... }: Executes its code immediately as soon as the block is parsed during the compile phase, before the rest of the script is compiled or executed. Useful for modifying@INCor verifying dependencies early.END { ... }: Executes as late as possible, right before the Perl interpreter exits. Useful for cleanup tasks, closing database connections, or deleting temporary files, even if the script terminates viadie().
44. Simulate the behaviour of ‘use lib’ pragma using BEGIN block.Theuse lib '/path/to/dir'pragma simply unshifts a directory path onto the@INCarray at compile time. You can manually simulate this behavior exactly like this:BEGIN { unshift @INC, '/path/to/dir'; }
File Handling in Perl
- 45. File Testing. Binary or text, size, accessed like –e, -f, -a, -c –m etcPerl provides a suite of file test operators that perform checks on files and directories, returning true or false (or specific values like size or age).
-e: File or directory exists.-f: Is a plain file.-d: Is a directory.-s: Returns the size of the file in bytes (and evaluates to true if the size is greater than zero).-T/-B: Tests if the file is a text (-T) or binary (-B) file.-M: Returns the script's start time minus the file's last modification time, in days.-A: Returns the script's start time minus the file's last access time, in days.-C: Returns the script's start time minus the file's inode change time, in days.
46. Use of the special file handle _. (Note: it's just _ not $_)When you perform a file test (like-eor-M) or astaton a file, Perl executes an expensive system call to fetch the file's metadata, which it then caches in memory. The special filehandle_(underscore) tells Perl to reuse the cachedstatinformation from the very last file test rather than making a fresh system call.Example:if (-f $file && -s _ < 1024)checks if it's a plain file, and if so, reuses the cache to check if its size is under 1KB, boosting performance significantly.47. Syntax for opening, appending, reading, writing filesModern Perl uses the three-argumentopenfunction with lexical filehandles:- Reading (
<):open(my $fh, '<', 'filename.txt') or die "Cannot open: $!"; - Writing (
>):open(my $fh, '>', 'filename.txt') or die "Cannot open: $!";(This truncates/overwrites existing content). - Appending (
>>):open(my $fh, '>>', 'filename.txt') or die "Cannot open: $!";(This adds to the end of an existing file).
48. How to assign File handle into an array instead of using while loop?You can read the entire contents of a filehandle into an array simply by evaluating the filehandle in a list context. Each element of the array will contain one line of the file (including the newline character).my @lines = <$fh>;(Note: This is memory-intensive for very large files, hence whywhile (<$fh>)is generally preferred).49. How to delete a file?Use the built-inunlinkfunction, which deletes a list of files and returns the number of files successfully deleted.unlink('file.txt') or die "Could not delete: $!";50. How to copy or move a file?Perl handles this best via the standardFile::Copymodule.- Copy:
use File::Copy; copy("source.txt", "destination.txt") or die "Copy failed: $!"; - Move:
use File::Copy; move("old_name.txt", "new_name.txt") or die "Move failed: $!";
(You can also use the built-inrename("old", "new")function to move a file, but it may fail if you try to move a file across different filesystem partitions, whereasFile::Copy::movehandles this gracefully).51. What the various file permissions (flags) in Perl?Permissions can be checked using specific file test operators, or modified using thechmodfunction.- Test Operators:
-r: File is readable by the effective uid/gid.-w: File is writable by the effective uid/gid.-x: File is executable by the effective uid/gid.-o: File is owned by the effective uid.
- Setting Permissions: Use
chmodwith an octal number.chmod(0755, 'script.pl') or die "Couldn't chmod: $!";
52. Given a file, count the word occurrence (case insensitive)You can read the file line by line, convert each line to lowercase, split it into words using a regular expression, and populate a hash to track the counts.my %word_count; open(my $fh, '<', 'text.txt') or die $!; while (my $line = <$fh>) { # Extract words (alphanumeric sequences), ignoring punctuation my @words = split(/\W+/, lc($line)); foreach my $word (@words) { $word_count{$word}++ if $word ne ''; # Skip empty strings } } # Display results while (my ($word, $count) = each %word_count) { print "$word: $count\n"; }53. Displaying file contents using hash in PerlThis typically implies reading a file containing key-value pairs (like a configuration file) and storing it in a hash for easy display or lookup.my %config; open(my $fh, '<', 'config.txt') or die $!; # Assuming file format is: Key=Value while (my $line = <$fh>) { chomp $line; my ($key, $value) = split(/=/, $line, 2); $config{$key} = $value if defined $key; } # Displaying the contents foreach my $k (keys %config) { print "$k => $config{$k}\n"; }
Web based & Perl CGI
- 54. What do you know about CGI.pm?CGI.pm is a large, historically significant Perl module used for building Common Gateway Interface (CGI) web applications. It provides a robust API for parsing incoming HTTP request data (handling both GET and POST variables seamlessly), managing cookies, and generating HTTP headers. While it also contains methods for generating HTML tags, this practice is largely outdated. Note that as of Perl 5.22, CGI.pm was removed from the Perl core distribution, and modern Perl web development typically favors frameworks like Mojolicious, Dancer, or Catalyst.55. How to set sessions and cookies and how to implement in Perl?
- Cookies: You implement cookies using the
CGImodule to generate a cookie object, which is then passed to the HTTP header.use CGI; my $q = CGI->new; my $cookie = $q->cookie( -name => 'username', -value => 'alice123', -expires => '+1h' ); print $q->header(-cookie => $cookie); - Sessions: Sessions are generally implemented using the
CGI::Sessionmodule alongsideCGI.CGI::Sessionhandles the backend storage (like a file or database) and automatically generates a unique Session ID, which you send to the client via a cookie.use CGI; use CGI::Session; my $q = CGI->new; # Create a new session (or retrieve an existing one based on the incoming cookie) my $session = CGI::Session->new(undef, $q, {Directory=>'/tmp'}); # Store data on the server side $session->param('role', 'admin'); # Send the session ID to the user as a cookie my $cookie = $q->cookie(CGISESSID => $session->id); print $q->header(-cookie => $cookie);
56. How to display JSON based outputTo serve JSON, you need to use a module likeJSONorJSON::XSto serialize your Perl data structures (like hashes or arrays) into a JSON-formatted string. Crucially, you must also change the HTTPContent-Typeheader fromtext/htmltoapplication/json.use strict; use warnings; use JSON; my %data = ( status => 'success', user_id => 101, message => 'Data loaded successfully' ); # Convert the Perl hash to a JSON string my $json_output = encode_json(\%data); # Print the correct HTTP header, followed by the blank line, then the JSON print "Content-Type: application/json\n\n"; print $json_output;57. Why two \n in print “Content-type: text/html \n\n”;The HTTP protocol specification dictates that the HTTP response headers and the actual response body (the HTML content, JSON data, etc.) must be separated by a completely blank line.- The first
\nterminates theContent-type: text/htmlheader line. - The second
\ncreates the required blank line, signaling to the receiving web browser that the headers have ended and the page content is about to begin. Without the second newline, the browser will fail to render the page, treating your content as malformed headers resulting in an "Internal Server Error" or a blank screen.
Database oriented
- 58. How to connect database using DBI?To connect to a database in Perl, use the
DBI(Database Interface) module and call itsconnectmethod. You must provide a Data Source Name (DSN) string, the database username, the password, and optionally a hash reference of connection attributes (such as enabling exceptions withRaiseError).use DBI; my $dsn = "DBI:mysql:database=my_db;host=localhost"; my $username = "db_user"; my $password = "db_pass"; my %attributes = ( RaiseError => 1, AutoCommit => 1 ); my $dbh = DBI->connect($dsn, $username, $password, \%attributes) or die "Connection failed: " . DBI->errstr;59. What are the ways you know to get records from the table (fetchrow_hashref/arrayref)?After preparing and executing a SQL query to get a statement handle ($sth), you can fetch records using several methods depending on how you want the data structured:fetchrow_array(): Returns the next row of data as a standard Perl list (array) of values.my @row = $sth->fetchrow_array();fetchrow_arrayref()(or its aliasfetch()): Returns the next row as a reference to an array. It is faster and uses less memory thanfetchrow_array()because it avoids copying the data into a new list.my $array_ref = $sth->fetchrow_arrayref();fetchrow_hashref(): Returns the next row as a reference to a hash, where the hash keys are the database column names and the values are the row data. It is highly readable but slightly slower than array fetching.my $hash_ref = $sth->fetchrow_hashref();fetchall_arrayref(): Fetches all remaining rows from the query at once and returns a reference to an array of array references.my $all_data = $sth->fetchall_arrayref();fetchall_hashref('key_column'): Fetches all remaining rows and returns a reference to a hash of hash references, using a specified column's values as the primary keys for the outer hash.my $all_hash_data = $sth->fetchall_hashref('id');bind_columns(): For maximum performance, you bind specific Perl variables to the result columns before looping, so the variables are automatically updated on each call tofetch().
Object Oriented Perl
- 60. Diff b/w module and package
- Package: A namespace in Perl that separates your variables and subroutines from those in other namespaces to prevent naming collisions. It is declared using the
packagekeyword. - Module: A physical file (usually with a
.pmextension) that contains reusable Perl code. A single module file usually defines one package (with a matching name), but technically it can contain multiplepackagedeclarations.
61. Diff b/w class and package (rarely asked though )In Perl's traditional object-oriented system, there is no syntactical difference between a class and a package; a class is just a package. A package functions as a class when it contains subroutines designed to act as methods (where they expect an object reference or class name as their first argument) and when objects areblessed into its namespace.62. How do you call any subroutine in object oriented fashion? Hint: bless operatorTo call a subroutine in an object-oriented way, you must first create a reference (usually a hash reference) and use theblessoperator to associate it with a specific package (class).# In MyClass.pmpackage MyClass; sub new { my $class = shift; my $self = {}; bless $self, $class; # Associates the hash reference with MyClass return $self; } sub display { my $self = shift; # The object itself is passed as the first argument print "Hello from OOP\n"; } # In the calling script my $obj = MyClass->new(); $obj->display(); # Calls the subroutine in an OO fashion using the arrow operator63. Use base, EXPORT, EXPORT_OKuse base 'ParentClass': Establishes inheritance. It loads the specified module at compile-time and automatically addsParentClassto the current package's@ISAarray, meaning the current class will inherit methods fromParentClass.@EXPORT: An array used by theExportermodule. Any subroutines or variables listed here are exported automatically into the calling script's namespace when it runsuse YourModule;. This can cause namespace pollution.@EXPORT_OK: An array for on-demand exporting. Symbols listed here are only exported if the calling script explicitly requests them (e.g.,use YourModule qw(specific_function);).
64. Difference between ISA and EXPORT@ISA: Used for Object-Oriented inheritance. It defines the "is-a" relationship. If a method is called on an object but not found in its class, Perl looks through the classes listed in@ISAto find the method. It does not copy subroutines into your namespace.@EXPORT: Used for Procedural programming. It physically copies subroutines and variables from a module's namespace directly into the caller's namespace, allowing them to be called like local functions.
65. Use Base related questionsQuestions surroundinguse baseusually focus on how Perl handles multiple inheritance and method resolution. When you douse base qw(ClassA ClassB);, Perl searches for missing methods depth-first, left-to-right.(Note: In modern Perl development, theuse parentpragma is widely preferred overuse baseas it is lighter and avoids some historical edge cases associated withuse base).66. Use of AUTOLOAD function.AUTOLOADis a special fallback subroutine in Perl. If you attempt to call a method on an object or class, and Perl cannot find that method anywhere in the class or its@ISAhierarchy, it will check if anAUTOLOADsubroutine exists in the package. If it does, Perl invokesAUTOLOADinstead of throwing a fatal "Can't locate object method" error. The fully qualified name of the missing method is automatically stored in the special package variable$AUTOLOAD. It is frequently used for dynamically generating getters and setters for object attributes on the fly.
Regular Expression in Perl
- 67. IP Address validationA robust regular expression to validate an IPv4 address (ensuring each octet is strictly between 0 and 255):
my $ip = "192.168.1.1"; if ($ip =~ /^(?:(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.){3}(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)$/) { print "Valid IP\n"; }(For simple formatting checks without numeric range validation,^\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}$is sometimes used, but it is less accurate).68. Email id ValidationA basic regular expression to check standard email formatting:my $email = "test@example.com"; if ($email =~ /^[a-zA-Z0-9._%+-]+@[a-zA-Z0-9.-]+\.[a-zA-Z]{2,}$/) { print "Valid Email\n"; }(Note: For production environments, using a dedicated module likeEmail::Validis highly recommended over writing custom email regex).69. More than one @ is there. Give regex to store username and domain name after encountering last @ (Last @ will be the splitting point and more than one special characters can be there)Because Perl's*quantifier is greedy by default,.*will automatically consume everything up to the final@symbol in the string.my $str = "user\@name\@domain.com"; if (my ($username, $domain) = $str =~ /^(.*)@([^@]+)$/) { print "Username: $username\n"; # Outputs: user@name print "Domain: $domain\n"; # Outputs: domain.com }70. What are the uses of different modifiersModifiers are appended to the end of a regex (e.g.,/pattern/i) to change how the matching engine behaves:i(Case-insensitive): Ignores uppercase and lowercase distinctions.g(Global): Finds all matches in the string, rather than stopping after the first match.m(Multi-line): Changes^and$to match the start and end of any line within a multi-line string, rather than just the absolute start and end of the string.s(Single-line): Forces the dot (.) metacharacter to match newline characters (\n), treating the whole string as a single continuous line.x(Extended): Ignores whitespace within the regex pattern and allows comments (using#), making complex patterns readable.
71. Group, range, meta-character etc- Group
(): Parentheses group parts of a pattern together. They also "capture" the matched text into special memory variables ($1,$2, etc.) for later extraction or substitution. - Range
[]: Brackets define a character class, matching any single character contained within. You can define a range using a hyphen (e.g.,[a-z]matches any lowercase letter,[0-9]matches any digit). - Meta-character: Characters that hold special meaning to the regex engine rather than representing literal characters. Examples include
^,$,.,*,+,?,|,\,(,),[,],{,}. To use them literally, they must be escaped with a backslash (e.g.,\$).
72. m, s, trm//(Match): Used to search a string for a specific pattern. Themis optional if you use standard forward slashes (e.g.,/pattern/).s///(Substitute): Used to search for a pattern and replace it with a new string. (e.g.,s/old/new/).tr///ory///(Transliteration/Translation): Translates characters one-by-one. It does not use regular expressions. For example,tr/a-z/A-Z/replaces all lowercase letters with their uppercase equivalents.
73. Replace the last ‘x’ in a string with ‘ax’ in one regex. Example : abcxdefgxgaxa should become abcxdefgxgaaxaYou can achieve this effortlessly using a Negative Lookahead(?!...), which asserts that there are no morexcharacters ahead in the string:Perlmy $str = "abcxdefgxgaxa"; $str =~ s/x(?!.*x)/ax/; # Result: abcxdefgxgaaxaAlternatively, using greediness and capture groups:$str =~ s/(.*)x/$1ax/s;74. Perl regular expression is greedy. Can you explain it with one example?"Greedy" means quantifiers like*(zero or more) and+(one or more) will attempt to match as much text as physically possible while still allowing the overall pattern to succeed.- Example: Let
$str = "<div>Hello</div><div>World</div>"; - Greedy (Default): If we match
m/<div>(.*)<\/div>/, the.*is greedy. It grabsHello</div><div>World, stopping only at the absolute final closing tag. - Non-Greedy (Reluctant): By appending a
?quantifier, we make it lazy:m/<div>(.*?)<\/div>/. Now it stops at the very first closing tag it sees, capturing onlyHello.
75. Can you check palindrome condition using regular expression in perl? (Hint: regex doesn’t support recursion or counting facility)Based on traditional Regular Expression theory (and the constraint of the hint), no. Standard regular expressions represent finite state automata, which have no memory to "count" the length of a string or pair up nested boundary characters infinitely.(Note: While you can write a hardcoded regex for a palindrome of a specific, fixed length using backreferences like/(.)(.)(.)\2\1/, you cannot write a single traditional regex for a palindrome of arbitrary length. The standard Perl solution is to simply use the built-in string reverse function:if ($str eq reverse($str)) { ... }).
Perl One Liner
- 76. What is Perl one liner and where you will use it?A Perl one-liner is a short, concise Perl script designed to fit on a single command line and be executed directly from the terminal without creating a dedicated
.plfile. They are primarily used for quick text processing, data extraction, search-and-replace operations across multiple files, and system administration tasks where writing a full script would be overkill.77. What are the different options in Perl one Liner. Explain in details-e: Specifies that the immediate following string is the Perl code to execute.-n: Wraps the code in awhile (<>) { ... }loop, reading the input line by line into the$_variable, but does not automatically print anything.-p: Similar to-n, but it automatically prints the contents of$_after executing your code for each line.-i: Edits the file in-place. If you provide an extension (like-i.bak), it creates a backup of the original file before modifying it.-a: Autosplit mode. Used with-nor-p, it automatically splits the input line by whitespace and stores the result in the@Farray.-F: Specifies the delimiter pattern to use for the-aoption (e.g.,-F:splits by colons).-l: Enables automatic line-ending processing. It automaticallychomps the input lines and appends a newline character to anyprintstatements.
78. Add a blank line before every line.Use the-pflag and modify$_to include a leading newline:perl -pe 's/^/\n/' file.txt(Alternatively:perl -pe '$_ = "\n$_"' file.txt)79. Remove blank lines form a file.Use the-nflag and tell Perl to print the line only if it contains non-whitespace characters (\S):perl -ne 'print if /\S/' file.txt(To also remove lines that are completely empty but keep lines with spaces:perl -ne 'print if /./' file.txt)80. Print the total number of lines in a file (emulate wc -l).You can use the special variable$.which holds the current line number, and print it at theENDblock after the file has been fully read:perl -lne 'END { print $. }' file.txt81. Print the number of non-empty lines in a file.Increment a counter only if the line contains non-whitespace characters, and print it at the end:perl -lne '$count++ if /\S/; END { print $count+0 }' file.txt82. What option you will use to check syntax only without executing it?The-cflag checks the syntax of the script or one-liner and reports errors without actually running the program code:perl -c script.pl83. Which option is meant to enable all warnings or disable all warning inpite of using use warning or no warnings respectively?- Enable warnings: The
-wflag globally enables warnings, and-Wforces all warnings on unconditionally (overridingno warnings). - Disable warnings: The
-Xflag unconditionally disables all warnings, overriding anyuse warningspragmas inside the code.
Subscribe to:
Posts (Atom)