Perl: How to do a PHP-style include? [duplicate] - php

This question already has answers here:
Is there any way to "auto-use" certain modules everytime I write a script?
(2 answers)
How can I export a list of modules with my own module?
(1 answer)
Closed 7 years ago.
In a PHP include, code is parsed as if it was written right into the source file. You get the same result as if you literally copy and pasted file A into file B.
I'm looking for a way to do this in Perl. The number of libraries I have to 'use' in every script is getting annoyingly large, and especially annoying is having to use v5.14 just so I can have the say function. I'm actually looking up how to alter the interpreter to always use the latest version of Perl at this moment, compatibility with the rest of the world be damned, but this won't solve the rest of my list of includes.
Edit: none of the linked answers answer the question.

The equivalent of PHP's include is do EXPR. There is also require EXPR, which is like require_once, and use, which will also call import on the package.
However that is probably not what you want. If you have a lot of .pl scripts without packages, you are dealing with legacy code. You need to be carefull what you require and include where.

Related

php5 to php7 migration code problem using odd OOP references? [duplicate]

This question already has answers here:
Variable variables in classes using PHP 7
(2 answers)
Closed 1 year ago.
I'm trying to fix a friend's web site that needs to be updated to run on php7.2. It works fine on php5 but appears to crash with no errors when executing a command like this:
$$mod_name = &$$parent_array[$mod_parent]->addItem(new XNode($mod_name,$mod_url,"Images/doc.gif",false));
The weird variable reference like &$$ is not something I've seen before. Has this notation been depreciated in php7?
The code is using a library called xPandMenu. Here is the library:
https://www.phpclasses.org/package/2018-PHP-Generate-a-dynamic-hierarchic-menu.html
I reached out to the author of this code and he's not interested in updating it, and doesn't work with PHP much any more. I am not familiar with OOP and the odd variable/class references used.
Does anybody know what would cause this code to work fine in php5, but crash without error in php 7.2?
Here is the solution to this.
The way PHP7 parses double variable references is now different, according to this page:
https://www.php.net/manual/en/migration70.incompatible.php
So the proper re-writing of this:
&$$parent_array[$mod_parent]->addItem(new XNode($mod_name,$mod_url,"Images/doc.gif",false));
is:
&${$parent_array[$mod_parent]}->addItem(new XNode($mod_name,$mod_url,"Images/doc.gif",false));
Likewise a reference such as this in PHP5:
$$var['key'];
Behaves differently under PHP7 and must be hard-noted as this:
${$var['key']};

How to get the final php code to be executed replacing all the require and include with their code

First let me apologize if this question was answered before, I just don't seem to find the proper terms to search for it.
The context
I'm viewing some complex PHP code with a lot of require in it, it is kind of ineffective and time consuming to search for a function's or variable's definition through all the multiple level tree of files included in the current file.
The desired result :
so I'm wondering if a function exists that does the following:
Remove all those requires
Replace them with their code recursively
Output it on the browser or return it to a variable
In a nutshell, a function that prints the final PHP code to be executed.
The question :
Is there such a function? if yes, what is its name?
Thank you for the help.
You cannot get the code which is going to be executed because inclusion of code can be done at run-time, based on variables whose values you don't know before running the code.
What you can do is archive all the possible code to be executed, and pack it up as a phar archive.
Using modern tools, that could mean using composer archive.
More information available here https://getcomposer.org/doc/03-cli.md#archive
Besides, it would go against the ecosystem: in modern PHP applications, you use autoloading. Which means that even in projects with millions of lines of code and hundred of thousands of files, there is only one include/require statement.

PHP Include Function Efficiency [duplicate]

This question already has answers here:
Efficiency for including files of functions (in PHP)
(5 answers)
Closed 5 years ago.
If I use PHP's include function to import parts of the page that are repeated like navigation, header and footer for example - will that make the code more or less efficient or would it be the same?
Thanks
The include()-function reads the entire content of the file you are including, then execute it and then paste the result right where it is called. So regarding code speed, it would be less efficient. However, if you don't include extremely large files then it shouldn't make much of a difference.
I would agree that this is probably not something that you should concentrate on too much. Most of the time you can use includes freely without worrying about how fast it is. Ultimately, includes like this are for saving your time as a programmer.
Another factor to think about on the very low level is that the file you are accessing might be at a different place on the disk. So, that means that it may not be cached, and it will take longer to go to the file location than if it were located within the file itself.

How to encrypt a part of a PHP script [duplicate]

This question already has answers here:
encoding php scripts on fly [duplicate]
(4 answers)
Closed 9 months ago.
I have a PHP script,
for example
<?php
$name="Alfred";
echo $name;
?>
I used following script to encrypt,(by www.rightscripts.com/phpencode/index.php)
<?php
eval(gzinflate(str_rot13(base64_decode('encrypted code'))));
?>
But it only print "$name="Alfred";" and "Undefined variable $name".
What is the problem ? Is there any other solution ? Please help me ?
"Encoding" this way doesn't really protect you from anything.
The encoded PHP code is on the server, and so is the code that decodes it back to regular PHP.
If someone has sufficient access to your server that they can read your encoded PHP scripts, then it is almost certain that they also have sufficient access to read the decoder script. Which means your code isn't actually protected at all.
There are a number of obfuscators and encoders which can do what you want, but at the end of the day, all you're really doing to your code is slowing it down (eval() is a major performance killer, quite aside from its other issues).
A better solution might be to compile your code. There is a PHP compiler called HipHop which will do the trick for you. It's worth giving it a try.
Even with compiled code (in any language), it is still possible for someone who's determined to pull it apart and learn your secrets, but it'll be a lot harder than a simple encoded script, and also it should run faster than normal when compiled, compared with slower than normal when encoded, so you win both ways.

How can I create a language like PHP and how does it work? [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 8 years ago.
Improve this question
I want to know how to create a language like PHP. How does the server know how to translate a PHP file? How does it work? I want to do this for educational purposes. A simple language with basic features like echo, etc.
In basics, when webserver get's request to process php file it translating this request to php processor, it could be a module (like php.so in apache) or service (like php-cgi). Service or module compiles php script to machine code, executes it and gives the server the result of his job. You can download php source code from http://php.net. Source code include parser, thanslator, compiler and other components needed to execute php script as machine code
Divide your task in at-least two top level parts:
PHP as just another programming language - you need a lexical analyzer, parser and interpreter.
Libraries and modules for web servers likt Apache/TomCat to inteface with PHP.
This is a very big question. And not one which can really be answered here, but a few pointers to get you started...
PHP exposes some of its inner workings - if you have a look at:
<?php
$srctokens=token_get_all(file_get_contents(__FILE__));
print "<pre>\n";
foreach ($srctokens as $tok) {
print token_name($tok[0]) . ' -> ' . $tok[1];
}
?>
You'll see that it splits the code up into 'words' and assigns meta-data to each one (is it a function name, a variable, an operator...).
It then builds this into a tree structure, e.g. an 'if' statement might have three child nodes - an expression to evaluate (which itself would have child nodes), some code to evaluate if the expression is true, and some code to evaluate if the expression is false.
Typically it will use a set of built-in functions to read in data from the outside and to push data to the outside world.
Wikipedia or google are usually good starting points for finding out about stuff you don't know.
Its worth noting that because its an interpreted language, its simple to create executable code from PHP datastructures using eval() or create_function() (or just using temporary files). Indeed you could even implement a structured rewriting system such that the original data looks nothing like PHP code. e.g. lolcode
But you need to be very careful that you don't expose this mechanism to someone who might abuse it.
HTH
You could also write it in PHP, like BobX. Please note, BobX is actually an example of what NOT to do.

Categories